我试图按照events他们与用户提交的邮政编码和距离的距离来订购.
我附上了我的数据库表及其关系的示例,您可以看到geom与多个地址相关联,postcode并且地址可以与多个表相关联(在本例中为事件表).
我正在从最终用户那里获取一个邮政编码以及以英里为单位的半径来检索适当的事件,下面是我在Eloquent中实现这一目标的示例.
/**
* Extend locale method which initially only gets lat/long for given postcode to search
*
* @param \Illuminate\Database\Eloquent\Builder $query The query builder
* @param \App\Http\Requests\SearchRequest $request The search request
* @return void
*/
protected function locale(Builder $query, SearchRequest $request)
{
$postcode = $this->formatPostcode($request->postcode);
$geom = Geom::query()->where('postcode', $postcode)->first();
if (! $geom || Cache::has('postcodeAPIFailed')) {
return;
}
$lat = $geom->geo_location['lat'];
$long = $geom->geo_location['long'];
// Top-left point of bounding box
$lat1 = $lat …Run Code Online (Sandbox Code Playgroud)