How to Get User Location using IP Address in Laravel 12?
In this article, I will show you how to get current user location information from ip address in laravel 12 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.
Step for Laravel 12 Get User Location from IP Address Example
- Step 1: Install Laravel 12
- Step 2: Install stevebauman/location Package
- Step 3: Create Route
- Step 4: Create Controller
- Step 5: Create Blade File
- Run Laravel App
Step 1: Install Laravel 12
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 - ItSolutionStuff.com</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/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 12?- 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
Preview:
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Generate Barcode in Laravel 12?
- Laravel 12 Simple Pagination Example Tutorial
- How to Generate App Key in Laravel 12?
- Laravel 12 Chart using Highcharts JS Example
- Laravel 12 Socialite Login with Google Account Example
- Laravel 12 ChartJS Chart Example Tutorial
- Laravel 12 REST API with Passport Authentication Tutorial
- Laravel 12 One to Many Eloquent Relationship Tutorial
- Laravel 12 Ajax CRUD Operation Tutorial
- Laravel 12 Create Custom Error Page Example
- Laravel 12 Generate Fake Data using Factory Tinker Example
- Laravel 12 Clear Cache of Route, View, Config, Event Commands
- Laravel 12 Image Upload Example Tutorial
- How to Install PHP DOM Extension in Ubuntu 22.04?