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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners