Searching a table in MySQL based on latitude and longitude columns. But how do you make this as efficient as possible? So, Aaron Francis from PlanetScale made a video about this: Here is the scope you can use in your Laravel application: public function scopeDistanceSphere($query, $lat, $lng, $radiusInMeter = 100) { $offsetOneMeter = 0.000111; $radiusOffset = ($radiusInMeter * $offsetOneMeter) + $offsetOneMeter; $latOffsetMinus = $lat - $radiusOffset; $latOffsetPlus = $lat + $radiusOffset; $lngOffsetMinus = $lng - $radiusOffset; $lngOffsetPlus = $lng + $radiusOffset; return $query ->whereBetween('lat', [$latOffsetMinus, $latOffsetPlus]) ->whereBetween('lng', [$lngOffsetMinus, $lngOffsetPlus]) ->whereRaw(" ST_distance_sphere( point($lng, $lat), point(lng, lat)) > $radiusInMeter "); } Thanks to my colleague https://nl.linkedin.com/in/coen-de-rijter-220b82231, for making this scope.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners