$model = DB::table('table_name');
public static function scope_distance($model, $from_latitude, $from_longitude, $distance)
{
$raw = DB::raw("round(st_distance_sphere(point($from_latitude, $from_longitude),point(latitude , longitude)), 2)/1000 AS distance");
return $model->select('*')->addSelect($raw)->orderBy('distance', 'ASC')->having('distance', '<=', $distance);
}
注:st_distance_sphere 函数详见MySQL5.7版本说明
参:https://dev.mysql.com/doc/refman/5.7/en/spatial-convenience-functions.html