Laravel属于很多关系

时间:2020-04-02 12:02:06

标签: php laravel eloquent relationship

通过自定义数据透视表(related_teams)在同一模型(团队模型)上具有一个EmiratesToMany关系

related_teams table structure

我的关系如下所示

enter image description here

现在附加和分离工作正常,但是如您所见,每个团队都属于一个城市 。 所以我的问题是如何从那些附属团队中获取不同城市的列表

1 个答案:

答案 0 :(得分:0)

这取决于您要达到的目标,例如

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

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

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

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