我正在使用 laravel 和 eloquent。
实际上,我在根据另一个表的属性条件从表中过滤结果时遇到了问题。
我有3张 table :

  • 会场
  • 奥 git 里市

    以下是关系:
    一个 city 有很多 locations 并且一个 location 属于一个 city
    一个 location 属于一个 venue 并且一个 venue 有一个 location
    我在位置表上有一个 city_id 属性,您可以从关系中找出它。
    问题很简单:
    我怎样才能得到属于特定城市的那些场馆?
    我期望的 Eloquent 查询如下所示:$venues=Venue::with('location')->where('location.city_id',$city->getKey());当然这行不通,但似乎这是一项常见任务,并且会有一个 Eloquent 命令。
    谢谢!

    最佳答案

    有两个选择:

    $venues = Venue::whereIn('location_id', Location::whereCityId($city->id)->get->lists('id'))
        ->get();
    

    或者可能使用 whereHas :
    $venues = Venue::whereHas('location', function($query) use ($city) {
        $query->whereCityId($city->id);
    })->get();
    

    10-04 21:26
    查看更多