How to get latitude longitude from mysql in laravel 5?

By Hardik Savani | March 8, 2016 | | 4805 Viewer | Category : Laravel MySql


Share this post:



Whenever you need to get nearest posts from lat and long and if you are working on laravel query builder. then you can get distance easily using DB::raw(), so you can learn from following example how to get nearest value from mysql laravel.

Example:

$lat = 1;

$lon = 1;

DB::table("posts")

->select("posts.id"

,DB::raw("6371 * acos(cos(radians(" . $lat . "))

* cos(radians(posts.lat))

* cos(radians(posts.lon) - radians(" . $lon . "))

+ sin(radians(" .$lat. "))

* sin(radians(posts.lat))) AS distance"))

->groupBy("posts.id")

->get();