In this laravel tutorial you will learn how to get user location from ip address with step by step example. we will use the stevebauman/location package to get the current user’s location from an IP address. With this package, we can easily fetch details like country, country code, region/state, state name, city, postal code, latitude, longitude, continent, timezone, and currency.
Table of Contents
In this tutorial, we’ll see in laravel 12 how to get user location from IP address with a practical example.
What is IP Geolocation?
IP geolocation is the process of determining the geographical location of a device connected to the internet using its IP address. This technology can provide information such as country, region, city, postal code, latitude, longitude, timezone, and ISP details.
Why Get User Location in Laravel?
Implementing IP geolocation in your Laravel 12 application enables several valuable features:
- Personalized Content: Display region-specific offers, currency, and language preferences
- Security: Detect suspicious login attempts from unusual locations
- Analytics: Track visitor demographics and geographical distribution
- Compliance: Implement geo-blocking for regulatory requirements
- User Experience: Auto-fill location fields in forms and provide relevant local information
Why use stevebauman/location
The stevebauman/location package is one of the most popular and easy-to-use solutions for IP geolocation in Laravel. Here’s why it’s a good choice:
- Simple to use : Get complete location details with just one line of code.
- Multiple drivers supported : Works with free services like ip-api, ipinfo, or paid services like ipstack.
- Customizable : You can easily switch drivers or configure fallbacks if one service fails.
- Returns detailed data : Provides information such as country, region, city, postal code, latitude, longitude, timezone, and more.
- Perfect for analytics & personalization : Useful for showing local content, saving location for users, or tracking visitors.
Prerequisites
- Laravel 12 installation with PHP 8.1+ and Composer
- Basic understanding of Laravel controllers, routes, and Blade templates
Steps for Laravel 12 How to Get User Location from IP Address Example
- Step 1: Create a Fresh Laravel 12 Project
- Step 2: Install stevebauman/location Location Package
- Step 3: Add Route for Testing
- Step 4: Create Location Controller
- Step 5: Create a Blade View
- Step 6: Test in Browser
Step 1: Create a Fresh Laravel 12 Project
In this step we will install a fresh laravel project , If you don’t already have a project, create one using the following command given below:
composer create-project laravel/laravel get-user-location
Move into your project directory:
cd get-user-location
Setup database in .env file
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Now, Run the following command to migrate:
php artisan migrate
Step 2: Install stevebauman/location Location Package
We will use the popular stevebauman/location package to get user location details such as country, region, city, latitude, longitude, and more. Run the command given below:
composer require stevebauman/location
This installs the Location facade and service provider with auto-discovery in Laravel 12.
Publish the config:
php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"
This will create a config/location.php file where you can manage settings like drivers, fallbacks, and testing mode are configured.
Configure drivers and fallbacks
Open the config/location.php file and set your preferred driver along with fallbacks. This allows Laravel to try another provider if the first one fails. By default, the package uses IpApi (Stevebauman\Location\Drivers\IpApi::class) as the primary driver, and it’s safe to replace it with any supported driver class or a custom one.
In this tutorial, I’m using the default driver, but you can configure your preferred driver and fallbacks based on your needs.
<?php
return [
/*
|--------------------------------------------------------------------------
| Driver
|--------------------------------------------------------------------------
|
| The default driver you would like to use for location retrieval.
|
*/
'driver' => Stevebauman\Location\Drivers\IpApi::class,
/*
|--------------------------------------------------------------------------
| Driver Fallbacks
|--------------------------------------------------------------------------
|
| The drivers you want to use to retrieve the user's location
| if the above selected driver is unavailable.
|
| These will be called upon in order (first to last).
|
*/
'fallbacks' => [
Stevebauman\Location\Drivers\Ip2locationio::class,
Stevebauman\Location\Drivers\IpInfo::class,
Stevebauman\Location\Drivers\GeoPlugin::class,
Stevebauman\Location\Drivers\MaxMind::class,
],
A reliable setup could look like this:
- Primary: MaxMind local database (great for privacy and full control).
- Fallbacks: Services like ip-api, ipinfo, or others for better reliability.
Important testing note
By default, the package’s testing.enabled may return a U.S.-based test IP instead of the real client IP for local development; disable testing mode in config/location.php to use the real request IP or set it directly in the .env file.
LOCATION_TESTING=false
Step 3: Add Route for Testing
Open routes/web.php and add the following route:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LocationController;
Route::get('/location', [LocationController::class, 'show']);
Step 4: Create Location Controller
Run the command given below to create a LocationController
php artisan make:controller LocationController
Now, Open App\Http\Controllers\LocationController.php
<?php
namespace App\Http\Controllers;
use Stevebauman\Location\Facades\Location;
use Illuminate\Http\Request;
class LocationController extends Controller
{
public function show(Request $request)
{
// Get client IP from the request
$ip = $request->ip();
// Resolve position
$position = Location::get($ip);
return view('location', compact('position'));
}
}
Instead of Location::get($ip), you can simply use Location::get(). The Location::get() method automatically uses request()->ip() if no IP is provided, and it returns false if the lookup fails. If you try to access an attribute (for example, $position->countryName) without checking the result first, it will throw an error.

Common Issues
If you’re testing with localhost (127.0.0.1) or private IP (192.168.x.x) : will always return false.
Some IPs (like Cloudflare ones 162.159.24.227) may not return details depending on the lookup driver configured in config/location.php.
Free services like ip-api sometimes block requests after too many calls.
Issue: Behind proxy/load balancer getting wrong IP
If your application is behind Cloudflare, a CDN, or a load balancer, make sure Trusted Proxies are configured correctly. This ensures that request()->ip() returns the real client IP instead of the proxy address, which is necessary for accurate geolocation. You can read this blog to learn more: Laravel 12 How to Get Client IP Address Example.
Step 5: Create a Blade View
Create a new file resources/views/location.blade.php and add:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Laravel 12 User Location - ItStuffSolutiotions.io</title>
<!-- Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background: #f8fafc; font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial; }
.card-lg { border-radius: .9rem; box-shadow: 0 6px 30px rgba(16,24,40,0.08); }
.muted { color: #6c757d; }
.value { font-weight: 600; }
</style>
</head>
<body>
<div class="container py-5">
<div class="row justify-content-center">
<div class="col-12 col-md-12 col-lg-10">
<div class="card card-lg overflow-hidden">
<div class="card-body p-4 p-md-5">
<div class="d-flex align-items-start justify-content-between mb-3">
<div>
<h3 class="mb-0">Laravel 12 How to Get User Location from IP Address Example - ItStuffSolutiotions.io</h3>
<p class="muted mb-0">Get location info from IP using <code>stevebauman/location</code>.</p>
</div>
</div>
@if($position)
<div class="row g-3">
<div class="col-12 col-md-6">
<div class="p-3 bg-white rounded">
<h6 class="text-uppercase muted mb-2">IP & Provider</h6>
<p class="mb-1"><span class="muted">Client IP:</span> <span class="value">{{ $position->ip ?? '-' }}</span></p>
<p class="mb-0"><span class="muted">ISP / Provider:</span> <span class="value">{{ $position->isp ?? '-' }}</span></p>
</div>
</div>
<div class="col-12 col-md-6">
<div class="p-3 bg-white rounded">
<h6 class="text-uppercase muted mb-2">Country & Region</h6>
<p class="mb-1"><span class="muted">Country:</span> <span class="value">{{ $position->countryName ?? '-' }}</span> <small class="text-muted">({{ $position->countryCode ?? '-' }})</small></p>
<p class="mb-0"><span class="muted">Region / State:</span> <span class="value">{{ $position->regionName ?? '-' }}</span> <small class="text-muted">({{ $position->regionCode ?? '-' }})</small></p>
</div>
</div>
<div class="col-12">
<div class="p-3 bg-white rounded">
<h6 class="text-uppercase muted mb-2">City & Postal</h6>
<div class="row">
<div class="col-12 col-md-6">
<p class="mb-1"><span class="muted">City:</span> <span class="value">{{ $position->cityName ?? '-' }}</span></p>
<p class="mb-0"><span class="muted">Postal / ZIP:</span> <span class="value">{{ $position->zipCode ?? $position->postalCode ?? '-' }}</span></p>
</div>
<div class="col-12 col-md-6">
<p class="mb-1"><span class="muted">Time Zone:</span> <span class="value">{{ $position->timeZone ?? $position->timezone ?? '-' }}</span></p>
<p class="mb-0"><span class="muted">Local Time (if available):</span> <span class="value">{{ isset($position->timeZone) ? now()->setTimezone($position->timeZone)->format('d M Y, H:i') : '-' }}</span></p>
</div>
</div>
</div>
</div>
<div class="col-12">
<div class="p-3 bg-white rounded">
<h6 class="text-uppercase muted mb-2">Coordinates & Time</h6>
<div class="row">
<div class="col-12 col-md-6">
<p class="mb-1"><span class="muted">Latitude:</span> <span class="value">{{ $position->latitude ?? '-' }}</span></p>
<p class="mb-0"><span class="muted">Longitude:</span> <span class="value">{{ $position->longitude ?? '-' }}</span></p>
</div>
<div class="col-12 col-md-6">
<p class="mb-1"><span class="muted">Continent:</span> <span class="value">{{ $position->continent ?? '-' }}</span></p>
<p class="mb-0"><span class="muted">Currency:</span> <span class="value">{{ $position->currency ?? '-' }}</span></p>
</div>
</div>
</div>
</div>
<div class="col-12 text-end">
<small class="text-muted">Note: Use a public IP for testing (not 127.0.0.1 / 192.168.x.x). Disable package testing mode to get the real client IP.</small>
</div>
</div>
@else
<div class="alert alert-warning mb-0">
<h6 class="mb-1">Location not found</h6>
<p class="mb-0">Could not determine location from the provided IP. Make sure you are testing with a public IP and that the package testing mode is disabled in <code>config/location.php</code>.</p>
</div>
@endif
</div>
</div>
<div class="text-center mt-3 text-muted small">
© {{ date('Y') }} ItStuffSolutiotions.io — Laravel 12 example
</div>
</div>
</div>
</div>
<!-- Bootstrap 5 JS (optional) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Step 6: Test in Browser
In this step, we will test our application. Start the Laravel development server by running the following command:
php artisan serve
Open your browser, enter the given URL, and check the result as shown below.
http://127.0.0.1:8000/location

Conclusion
In this tutorial, we learned how to get user location from IP address in Laravel 12 using the stevebauman/location package. You can extend this by saving location details into the database for analytics, showing nearby services, or even restricting access based on location.