【问题标题】:Laravel query builder join whereLaravel 查询构建器加入哪里
【发布时间】:2018-05-19 17:30:48
【问题描述】:

我有这个查询:

$query = QueryBuilder::for(Advertisement::class)
            ->with('locations');

广告的位置方法如下所示:

public function locations()
{
    return $this->belongsToMany(Location::class, 'advertisement_locations', 'advertisement_id', 'location_id');
}

因此,介于两者之间的广告belongsToMany 位置是一个名为advertisement_locations 的数据透视表。

现在我只会获得longlatitude 之间的广告,这些广告位于locations 表中。

我怎么能这样做?

【问题讨论】:

    标签: php laravel eloquent query-builder


    【解决方案1】:

    使用这个:

    $query = Advertisement::whereHas('locations', function($query) {
        $query->whereBetween('long', [$min, $max])
            ->whereBetween('latitude', [$min, $max]);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-04
      • 2016-07-30
      • 2018-05-21
      • 2017-04-17
      • 2021-09-25
      • 2018-08-12
      • 2018-09-08
      • 2013-08-21
      相关资源
      最近更新 更多