Laravel 查询生成器加入哪里

Laravel query builder join where

我有这个查询:

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

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

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

因此中间的广告 belongsToMany 位置是一个名为 advertisement_locations 的支点 table。

现在我只会在 locations table.

上的给定 longlatitude 之间获取广告

我该怎么做?

使用这个:

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