简体   繁体   English

Laravel查询构建器加入哪里

[英]Laravel query builder join where

I've got this query: 我有这个查询:

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

The locations method on Advertisement looks like this: 广告上的locations方法如下所示:

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

So a advertisements belongsToMany locations in between is a pivot table called advertisement_locations . 因此,一个广告belongsToMany之间被称为透视表的位置advertisement_locations

Now I would only get the advertisements between a given long and latitude that's on the locations table. 现在,我只想得到一个给定的广告longlatitude那是在locations表。

How could I do this? 我该怎么办?

Use this: 用这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM