Forked from stevenmaguire/laravel-nearby-locations-query-scope.php
Created
November 15, 2017 12:20
-
-
Save be-geeky/3fe571774158bf16f23e2ca07fe91631 to your computer and use it in GitHub Desktop.
Revisions
-
Steven Maguire revised this gist
Aug 23, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ <?php /** * Query builder scope to list neighboring locations * within a given distance from a given location -
Steven Maguire renamed this gist
Aug 23, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Steven Maguire revised this gist
Aug 12, 2014 . 1 changed file with 28 additions and 28 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,28 +1,28 @@ /** * Query builder scope to list neighboring locations * within a given distance from a given location * * @param Illuminate\Database\Query\Builder $query Query builder instance * @param mixed $lat Lattitude of given location * @param mixed $lng Longitude of given location * @param integer $radius Optional distance * @param string $unit Optional unit * * @return Illuminate\Database\Query\Builder Modified query builder */ public function scopeDistance($query, $lat, $lng, $radius = 100, $unit = "km") { $unit = ($unit === "km") ? 6378.10 : 3963.17; $lat = (float) $lat; $lng = (float) $lng; $radius = (double) $radius; return $query->having('distance','<=',$radius) ->select(DB::raw("*, ($unit * ACOS(COS(RADIANS($lat)) * COS(RADIANS(latitude)) * COS(RADIANS($lng) - RADIANS(longitude)) + SIN(RADIANS($lat)) * SIN(RADIANS(latitude)))) AS distance") )->orderBy('distance','asc'); } -
Steven Maguire created this gist
Aug 6, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ /** * Query builder scope to list neighboring locations * within a given distance from a given location * * @param Illuminate\Database\Query\Builder $query Query builder instance * @param mixed $lat Lattitude of given location * @param mixed $lng Longitude of given location * @param integer $radius Optional distance * @param string $unit Optional unit * * @return Illuminate\Database\Query\Builder Modified query builder */ public function scopeDistance($query, $lat, $lng, $radius = 100, $unit = "km") { $unit = ($unit === "km") ? 6378.10 : 3963.17; $lat = (float) $lat; $lng = (float) $lng; $radius = (double) $radius; return $query->having('distance','<=',$radius) ->select(DB::raw("*, ($unit * ACOS(COS(RADIANS($lat)) * COS(RADIANS(latitude)) * COS(RADIANS($lng) - RADIANS(longitude)) + SIN(RADIANS($lat)) * SIN(RADIANS(latitude)))) AS distance") )->orderBy('distance','asc'); }