laravel get distance between two locations code example

Example 1: laravel get distance between two locations

$sqlDistance = DB::raw('( 111.045 * acos( cos( radians(' . $data['lat'] . ') )        * cos( radians( addresses.latitude ) )        * cos( radians( addresses.longitude )        - radians(' . $data['lng']  . ') )        + sin( radians(' . $data['lat']  . ') )        * sin( radians( addresses.latitude ) ) ) )');

Example 2: laravel get distance between two locations

$stores =  DB::table('stores')        ->join('addresses','addresses.id','stores.address_id')        ->select('addresses.latitude',                   'addresses.longitude','stores.*')        ->selectRaw("{$sqlDistance} AS distance")        ->orderBy('distance')        ->get();

Tags:

Php Example