0
votes

Having a belongsToMany relation on the same model (Team Model) through custom pivot table (related_teams)

related_teams table structure

and my relation is like the following

enter image description here

now attaching and detaching is working just fine , however as you see every team belongs to a city . so my question is how can i get a list of distinct cities from those attached teams

1

1 Answers

0
votes

It depends what you want to achive but for example

$this->attachedHotels->groupBy('city_id');

https://laravel.com/docs/6.x/collections#method-groupby

or

$this->attachedHotels->pluck('city_id')->unique();

https://laravel.com/docs/6.x/collections#method-pluck https://laravel.com/docs/6.x/collections#method-unique