We all know that get calculate use distance between two places using google maps, an event it's better, it will show the exact direction between two-point A and B, But what to do if we want to calculate that two-point distance from BackEnd.
You need to calculate that distance in the laravel query, But must need to store that user's current latitude and longitude and then calculate that user distance by given latitude and longitude from little logical query in laravel.
The calculation of the distance in the laravel query is a little bit difficult to remember. So it always needs to search and find it out.
The query will retrieve all the record distances by calculating miles between two points and can ordering that distance ASC or DSC.
So in this post, I will give you an example of how to find the distance between lat and long in the using laravel query.
The below DB::raw will give you the distance between that points.
Simple Query Distance Calculating
Simple db raw calculation component.
DB::raw("6371 * acos(cos(radians(" . $latitude . ")) * cos(radians(doctors.latitude)) * cos(radians(doctors.longitude) - radians(" . $longitude . ")) + sin(radians(" .$latitude. ")) * sin(radians(doctors.latitude))) AS distance");
Eloquent Full Example
Here is the full example through the routing params receiving in first param latitude and second param longitude.
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ use App\Models\User; Route::get('/user/{latitude}/{longitude}', function ($latitude,$longitude) { $user = User::select( 'id', 'name', 'email', 'user_type', 'age', 'dob', 'verification', DB::raw("6371 * acos(cos(radians(" . $latitude . ")) * cos(radians(doctors.latitude)) * cos(radians(doctors.longitude) - radians(" . $longitude . ")) + sin(radians(" .$latitude. ")) * sin(radians(doctors.latitude))) AS distance"), ) ->where('status',1) ->get(); return view('user.list',compact('user')); });
Example URL
This is my localhost will send a request with lat and long.
http://127.0.0.1:8000/user/22.754565/72.562454
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Laravel Event Broadcast With Redis Socket.io Echo To Listen Real-Time Message
- How To Integrate Stripe Payment Gateway in Laravel 9
- Laravel One to Many Eloquent Relationship Tutorial With Example
- Laravel 6 validation with error message
- How To Change ENUM DataType Without Losing Data Laravel Migration
- Firebase Dynamic Links Shorten URL PHP Function
- Attributes Set In Laravel Model
- Laravel 10 PDF Export Example With DomPDF
- Filtering data excel export in laravel example
- Thawani Pay Ecommerce API Integration In Laravel