How to get latitude longitude from mysql in Laravel?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

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.

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();

Read Also :Laravel Join with Subquery in Query Builder Example

Thanks for read. I hope it help you. For more you can follow us on facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →