Today now in this post i will show you how to get latitude longitude from mysql in laravel. Whenever we need to get the nearest posts from the lat and long and if we are working on laravel query builder. Then we can get the distance easily by using DB::raw(), so here you can learn from the following example how to get nearest value from mysql in laravel application.
$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();
Thanks for read. I hope it help you. For more you can follow us on facebook.