-1

I use this tool to calculate distance between 2 points:

$location = new CnpPoi(28.535153, -81.383219);
$destination = new CnpPoi(42.594018, -88.433886)
$location->getDistanceInMilesTo($destination);
$location->getDistanceInMetersTo($destination);

need to use it inside a query to get store ordered by distance from a given point that already have passed trough url his $lat e $long. How i can use this function inside a query?

rubenSousa
  • 261
  • 8
  • 23

1 Answers1

0
public function getStoresNear($latitude, $lngitude){

    $stores = Store::select('stores.*')
        ->selectRaw('( 6371 * acos( cos( radians(?) ) *
                           cos( radians( store_lat ) )
                           * cos( radians( store_long ) - radians(?)
                           ) + sin( radians(?) ) *
                           sin( radians( store_lat ) ) )
                         ) AS distance', [$latitude, $longitude, $latitude])
        ->orderBy('distance', 'desc')
        ->get();

    return $stores;
}
Paras
  • 9,258
  • 31
  • 55
  • Syntax error 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server- near 'long ) - radians(?) ) + sin( radians(?) ) * ' at line 3 (SQL: select `tb_users`.*, ( 6371 * acos( cos( radians(41.9242814) ) * cos( radians( lat) ) * cos( radians( long ) - radians(12.4923974) ) + sin( radians(41.9242814) ) * sin( radians( lat ) ) ) ) AS distance from `tb_users` order by `distance` desc) (View: /home/gethelpme/public_html/local/resources/views/user/risultati.blade.php) (View: /home/gethelpme/public_html/local/resources/views/user/risultati.blade.php) – rubenSousa Jan 18 '17 at 20:12
  • Oh, sorry this answer was meant for mysql not MariaDB. Please post the mariaDb tag in your question – Paras Jan 18 '17 at 20:15