Meh*_*kri 7 foreign-collection laravel eloquent laravel-5
我在用laravel和雄辩。
实际上,我在基于另一个表的属性条件筛选表结果时遇到问题。
我有3张桌子:
市
这里是关系:
a city有很多locations,a location属于a city。
a location属于a venue而a venue具有一个location。
我city_id在位置表上有一个属性,您可以从关系中找出来。
问题很简单:
如何获得属于特定城市的场地?
我期望的雄辩的查询如下所示:
$venues=Venue::with('location')->where('location.city_id',$city->getKey());
当然这行不通,但似乎这是常见的任务,并且会有雄辩的命令。
谢谢!
有两个选择:
$venues = Venue::whereIn('location_id', Location::whereCityId($city->id)->get->lists('id'))
->get();
Run Code Online (Sandbox Code Playgroud)
或可能使用whereHas:
$venues = Venue::whereHas('location', function($query) use ($city) {
$query->whereCityId($city->id);
})->get();
Run Code Online (Sandbox Code Playgroud)