Laravel Calculate Distance Between Two Latitude and Longitude Example
This example is focused on how to calculate distance between two latitude and longitude in laravel. In this article, we will implement a laravel calculate distance between two coordinates. I would like to show you calculate distance between two points in laravel. I’m going to show you about calculate distance between two coordinates laravel.
You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.
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.
class ProductController extends Controller
* Write code on Method
* @return response()
public function index(Request $request)
$lat = 51.0258751;
$lon = 4.4775352;
,DB::raw("6371 * acos(cos(radians(" . $lat . "))
* cos(radians(users.lon) - radians(" . $lon . "))
+ sin(radians(" .$lat. "))
* sin(radians(users.lat))) AS distance"))
dd("Users get successfully.");
I hope it can help you...
We are Recommending you
- How to Search First Name and Last Name in Laravel Query?
- Laravel Group By with Max Value Query Example
- Laravel Eloquent orWhereHas() Condition Example
- Laravel Search Case Insensitive Query Example
- Laravel Eloquent When Condition Example
- PHP Laravel Inner Join Query Example
- Laravel Eloquent whereNotBetween() Query Example
- Laravel Eloquent selectRaw() Query Example
- Laravel Has Many Through Eloquent Relationship Tutorial
- Laravel - How to search with comma separated values using Query Builder ?
- Laravel Query Builder Where Exists Example
- Example of unionAll in Query Builder Laravel
- Laravel Join with Subquery in Query Builder Example