我的数据库:
|----|------|------|
| id | lat | long |
|----|------|------|
当我使用此代码时:
public function near($lat ,$lng)
{
$cities = map::select(DB::raw('*, ( 6367 * acos( cos( radians('.$lat.') ) * cos( radians( lat ) ) * cos( radians( long ) - radians('.$lng.') ) + sin( radians('.$lat.') ) * sin( radians( lat ) ) ) ) AS distance'))
->where('distance', '<', 25)
->orderBy('distance')
->get();
return response($cities);
}
它给我错误:
SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有一个错误;请参见语法。检查与您的MySQL服务器版本对应的手册以获取正确的语法以在第1行附近使用'long)-radians(5))+ sin(radians(28))* sin(radians(lat))))AS distan' (SQL:选择*,(6367 * acos(cos(radians(28))* cos(radians(lat))* cos(radians(long)-radians(5))+ sin(radians(28))* sin( radians(lat)))))与
maps
的AS距离,其中distance
<25依次为distance
asc)
答案 0 :(得分:0)
您可以使用别名获取距离
$dist = "(6371 * acos(cos(radians($location->latitude))
* cos(radians(model.latitude))
* cos(radians(model.longitude)
- radians($location->longitude))
+ sin(radians($location->latitude))
* sin(radians(model.latitude))))";
return $query
->select() //pick the columns you want here.
->selectRaw("{$dist} AS distance")
->whereRaw("{$dist} < 25");