In this tutorial you will learn about the Laravel 9 Get Current User Location From IP and its application with practical example.
In this Laravel 9 Get Current User Location From IP tutorial I will show you how to get user location information using user IP address in laravel 9. In this tutorial you will learn to get country name, country code, city name, and address from user IP address in laravel 9. When you working on web development project you come to situation where you may want to fetch user information such as country Name, country Code, region Code, region Name, city Name, zip Code, iso Code, postal Code, latitude, longitude, metro Code, metro Code from ip address in laravel 8. In this example I will help you to fetch user info from IP their ip address. We will be using stevebauman/location package in this example to fetch country name, country code, city name, and address from IP address.
How to Get Current User Location in Laravel 9
In this tutorial you will learn how to get current user location with ip address using stevebauman location package. The stevebauman/location laravel package is used to get users location data by their IP Address. This helps in retrieving user’s location information from IP address. The location library helps in retrieving following location information:
- Country name and code
- Region name and code
- City name
- Zipcode
- ISO code
- Postal code
- Latitude and longitude
- Metro code
Laravel 9 Get Current User Location From IP
- Install Laravel 9
- Connecting App to Database
- Install stevebauman/location Package
- Create Route
- Create Controller By Artisan Command
- Create Blade View
- Run Development Server
Install Laravel 9
First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Connecting App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database-name DB_USERNAME=database-user-name DB_PASSWORD=database-password |
Install stevebauman/location Package
In this step, we will install stevebauman/location Package via the composer dependency manager. Use the following command to install stevebauman/location Package.
1 |
composer require stevebauman/location |
Then, Go to config directory and open app.php file. And register this package into laravel 8 app by adding the following code into your app.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
'providers' => [ .... Stevebauman\Location\LocationServiceProvider::class, ], 'aliases' => [ .... 'Location' => 'Stevebauman\Location\Facades\Location', ] |
Create Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\UserController; /* |-------------------------------------------------------------------------- | 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! | */ Route::get('display-user', [UserController::class, 'index']); |
Create Controller By Artisan Command
Now, lets create a controller named UserController using command given below –
1 |
php artisan make:controller UserController |
Now, go to app\Http\Controllers
and open UserController.php file. Then put the following code into your UserController.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Stevebauman\Location\Facades\Location; class UserController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { /* $ip = $request->ip(); Dynamic IP address */ $ip = '162.159.24.227'; /* Static IP address */ $currentUserInfo = Location::get($ip); return view('user', compact('currentUserInfo')); } } |
Create Blade View
In this step we will create blade view file. Go to resources/views directory and create user.blade.php. Then add the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <h1>How to Get Current User Location with Laravel</h1> <div class="card"> <div class="card-body"> @if($currentUserInfo) <h4>IP: {{ $currentUserInfo->ip }}</h4> <h4>Country Name: {{ $currentUserInfo->countryName }}</h4> <h4>Country Code: {{ $currentUserInfo->countryCode }}</h4> <h4>Region Code: {{ $currentUserInfo->regionCode }}</h4> <h4>Region Name: {{ $currentUserInfo->regionName }}</h4> <h4>City Name: {{ $currentUserInfo->cityName }}</h4> <h4>Zip Code: {{ $currentUserInfo->zipCode }}</h4> <h4>Latitude: {{ $currentUserInfo->latitude }}</h4> <h4>Longitude: {{ $currentUserInfo->longitude }}</h4> @endif </div> </div> </div> </body> </html> |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000/display-user |