In this article, I will show you How to Get User Location using IP Address in Laravel 11 application.
In this tutorial, we will use the stevebauman/location composer package to get the current user location in a Laravel app. We will extract country name, country code, region code, region name, city name, ZIP code, latitude, and longitude from the IP address. Simply follow the steps below to achieve the layout provided. You Can Learn How to Create ZIP Archive File in Laravel 11?
How to Get User Location using IP Address in Laravel 11
Step for How to Get User Location using IP Address in Laravel 11 Example
Step 1: Install Laravel 11
This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Install stevebauman/location Package
Here, we will install the stevebauman/location package for getting the current location of the logged-in user.
composer require stevebauman/location
Step 3: Create Route
In this step, we need to create one route for getting user location information from an IP address.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
Route::get('user', [UserController::class, 'index']);
Step 4: Create Controller
In this step, we need to create a `UserController` and add the following code in that file:
app/Http/Controllers/UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Stevebauman\Location\Facades\Location;
use Illuminate\View\View;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request): View
{
$ip = $request->ip() // Static IP: $ip = '162.159.24.227';
$currentUserInfo = Location::get($ip);
return view('user', compact('currentUserInfo'));
}
}
Step 5: Create Blade File
Here, we need to create a blade file for the user. So, let’s create them one by one.
resources/views/user.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>How to Get Current User Location with Laravel</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="card mt-5">
<h3 class="card-header p-3">How to Get Current User Location with Laravel 11?- ItSolutionStuff.com</h3>
<div class="card-body">
@if($currentUserInfo)
<p><strong>IP:</strong> {{ $currentUserInfo->ip }}</p>
<p><strong>Country Name:</strong> {{ $currentUserInfo->countryName }}</p>
<p><strong>Country Code:</strong> {{ $currentUserInfo->countryCode }}</p>
<p><strong>Region Code:</strong> {{ $currentUserInfo->regionCode }}</p>
<p><strong>Region Name:</strong> {{ $currentUserInfo->regionName }}</p>
<p><strong>City Name:</strong> {{ $currentUserInfo->cityName }}</p>
<p><strong>Zip Code:</strong> {{ $currentUserInfo->zipCode }}</p>
<p><strong>Latitude:</strong> {{ $currentUserInfo->latitude }}</p>
<p><strong>Longitude:</strong> {{ $currentUserInfo->longitude }}</p>
@endif
</div>
</div>
</div>
</body>
</html>
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/user