【发布时间】:2019-07-18 03:24:11
【问题描述】:
我需要从表“properties”中获取数据,在该表中,我以经度和纬度以及其他字段(如 construction_type、no_of_bedrooms、no_of_bathrooms)存储房产的地址。 现在我需要根据给定的最近位置过滤数据,并传递其他过滤器,如 construction_type、no_of_bedrooms、no_of_bathrooms。
我正在使用 Haversine 公式来获取给定位置最近的位置。 在传递其他过滤器时,我在编写 laravel eloqent 查询时也很复杂。
$property = (new Property())->newQuery();
if(\Request::get('Lat')!=null && \Request::get('Lng')!=null){
$lat=\Request::get('Lat');
$lng=\Request::get('Lng');
$radius=200;
$q="( 3959 * acos( cos( radians(' . $lat . ') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(' . $lng . ') ) + sin( radians(' . $lat .') ) * sin( radians(lat) ) ) )";
$property->selectRaw("{$q} AS distance")->havingRaw("distance < ?", [$radius]);
}
if(\Request::get('construction_status')!=null){
$property->where('construction_status', \Request::get('construction_status'));
}
//other filters
return $property->get();
我希望结果是具有给定最近位置的属性和其他过滤器
【问题讨论】:
-
你有什么问题?您的任何其他过滤器是否致电
select()? -
我在使用
$property = (new Property())->newQuery();编写原始 sql 查询时遇到问题
标签: laravel eloquent haversine