How To Get Current User Location In Laravel | Laravel Tutorial | Appfinz Technologies
How to get current user location in laravel via user ip
In this tutorial, we will show you how to get the current user location in laravel using stevebauman/location, library. Sometimes we need to get the user location according to their IP address so first we will see how do we get it
so lets start coding now, first we will install the latest laravel into our system. at the time i am writting this blog Laravel Version is 9.3.4
So, here I am using the stevebauman/location laravel library package, Using this package you can get information of your user on your website it includes of their IP address, Country Name, Country Code, Postal code, Region, State Name, Country name, Longitude, Latitude etc.
Step 1: Install Laravel on your system
In this step create a fresh laravel project so, copy the below command and create a new laravel project if doesn’t exist.
composer create-project --prefer-dist laravel/laravel location
Step 2: Install stevebauman/location Package
After that project setup, we will install stevebauman/location Package using the below command.
composer require stevebauman/location
Step 3: Add Service Provider And Alias
After package installation, we need to add service provider and alias in config/app.php
Important: If you're using Laravel 5.5 or above, you can skip the registration of the service provider, as it is registered automatically.
- Stevebauman\Location\LocationServiceProvider::class
'providers' => [ Stevebauman\Location\LocationServiceProvider::class, ], 'aliases' => [ 'Location' => 'Stevebauman\Location\Facades\Location', ],
You can also do this by this way it will publish the configuration file (this will create a location.php
file inside the config/
directory):
php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"
Step 5: Add a route
Now, set the route file this way Add Routes
Route::get('/', [App\Http\Controllers\homeController::class, 'user'])->name('user');
Step 4: Create Controller
we will display the view and fetch user location using this library in our controller
php artisan make:controller homeController
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Stevebauman\Location\Facades\Location; class UserController extends Controller { public function userDetails() { $ip = '103.226.226.86'; //For static IP address get //$ip = request()->ip(); //Dynamic IP address get $data = \Location::get($ip); return view('welcome',compact('data')); } }
Step 6: Create a Blade file to show this
Now, by default there is blade file if not there then create welcome.blade.php file for get current user location details in this path resources\views\welcome.blade.php and add below html code.
<html> <head> <title>How To Get Current User Location In Laravel - Appfinz Technologies</title> </head> <body style="text-align: center;"> <h1> How To Get Current User Location In Laravel - Appfinz Technologies</h1> <div style="border:1px solid black; margin-left: 300px; margin-right: 300px;"> <h3>IP: {{ $data->ip }}</h3> <h3>Country Name: {{ $data->countryName }}</h3> <h3>Country Code: {{ $data->countryCode }}</h3> <h3>Region Code: {{ $data->regionCode }}</h3> <h3>Region Name: {{ $data->regionName }}</h3> <h3>City Name: {{ $data->cityName }}</h3> <h3>Zipcode: {{ $data->zipCode }}</h3> <h3>Latitude: {{ $data->latitude }}</h3> <h3>Longitude: {{ $data->longitude }}</h3> </div> </body> </html>
So, finally, we have got our result here, I hope you liked it !!