Calculating Distance using MySQL

While working on project I recently came across very complex SQL query which mainly includes distance calculations. Initially I started off with pretty basic Haversine Formula which worked really fine at first. Then suddenly I walked into very complex situation where I felt the Haversine is not enough so I started looking out for replacement of the current implementation. I came across MySQL Spatial Functions specially ST_DISTANCE_SPHERE function.
This is pretty basic to understand and way easier to implement either using raw sql query or using it in Laravel.

Using Raw SQL

Using Laravel Scopes

(This example shows how you can filter out results using spatial function in your model by providing $latitude and $longitude.

Hope this helps. Let me know any suggestions, doubts or questions.