Forked from LuenCC/gist:e8dcf4a38096617799f3002644012af6
Created
November 18, 2020 08:01
-
-
Save nadyshalaby/a6b0d3e98caf67e4e0c3a6d3741c531a to your computer and use it in GitHub Desktop.
Laravel find nearest location in km from lat, long database
This file contains 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 characters
private function findNearestLocation(Request $request) | |
{ | |
$location = DB::table('locations') | |
->select('name', 'latitude', 'longitude', 'region', DB::raw(sprintf( | |
'(6371 * acos(cos(radians(%1$.7f)) * cos(radians(latitude)) * cos(radians(longitude) - radians(%2$.7f)) + sin(radians(%1$.7f)) * sin(radians(latitude)))) AS distance', | |
$request->input('latitude'), | |
$request->input('longitude') | |
))) | |
->having('distance', '<', 50) | |
->orderBy('distance', 'asc') | |
->get(); | |
return $location; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment