Easily detect user country in Laravel using IP address with the stevebauman/location package. Boost geo-targeting, analytics and personalization with this seamless PHP integration.

Key Points

  • 65% of businesses use geolocation for targeted content and improved UX.
  • Geolocation boosts user engagement by 30% on average for location-aware apps.
  • Laravel powers over 1.2 million websites worldwide, many leveraging location services.
Digittrix Blog Author Image

Web Developer

Pragati S.

3 min read

3+ yrs of crafting high-performance websites with clean code and modern tech—turning ideas into smooth digital experiences.

 logo of Laravel and a php framework alongside a globe with a location pin with ip address

If you're building a custom web solution and want to offer personalized experiences based on user location, geolocation by IP is an essential feature. Laravel, a popular PHP framework, simplifies this process with packages like stevebauman/location. This guide explains how to get the user's country, city, and region using their IP address—an ideal solution for any website development company or team offering web development services.

Whether you're creating an e-commerce platform, SaaS dashboard, or a custom CRM, identifying the user’s location allows you to provide regional content, show appropriate currency, or restrict access based on location—powerful benefits for any custom website development project.

Want to speed up your app development? Explore 10 proven ways to make your React Native workflow super fast and boost productivity.

Step 1: Install stevebauman/location Package

To get started, you’ll need the stevebauman/location package, a powerful tool for fetching user location via IP address in Laravel.

Use Composer to install:

composer require stevebauman/location

                                        composer require stevebauman/location
                                        
                                    

This package works seamlessly with Laravel and is a must-have for companies focused on high-quality web development services.

Step 2: Set Up the Route

In your routes/web.php file, create a route that connects to your controller.

                                        use App\Http\Controllers\UsersController;
Route::get('get-country-from-ip', [UsersController::class, 'getCountry']);

                                        
                                    

This route will trigger the location detection process and return location data for the given IP address.

Step 3: Create the Controller Logic

Let’s define the controller logic to get geolocation data based on IP. This is useful for a website development company building applications that rely on regional information.

app/Http/Controllers/UsersController.php:

                                        <?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Stevebauman\Location\Facades\Location;
class UsersController extends Controller
{
    public function getCountry()
    {
        // /*  $ip = request()->getClientIp(); Dynamic IP address */
        $ip = '122.173.29.58'; /* Static IP address */
        $currentUserInfo = Location::get($ip);
        return view('Scripts.CountryDetailsFromIP', compact('currentUserInfo'));
    }
}
                                        
                                    

When working locally, the real IP may be shown as 127.0.0.1. Use a public static IP to test accurate geolocation.

Step 4: Create the Blade View

Now, create a Blade file to display the user’s location data. This UI step is especially valuable in projects offered by professional web development services and custom website development teams. resources/views/Scripts/CountryDetailsFromIP.blade.php: 

                                        <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Get Country With IP Address</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h4>How to Get Current User Location with Laravel</h4>
        <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>
                                        
                                    

Output

Once implemented, the output will display the user's geolocation data, including:

This information is displayed in a clean, user-friendly interface, making it easy for developers and businesses to use it for dynamic content, location-based services, or analytics. It’s a valuable feature in any application crafted by a professional website development company as part of their custom website development services.

Test and Deploy

Visit /get-country-from-ip in your browser. You should see a page displaying detailed geolocation data, including the user’s IP address, country, city, zip code, and geographic coordinates.

This geolocation approach can be further enhanced by integrating location data into user sessions, tailoring shopping preferences, implementing location-based redirection, or enabling content localization. These features are highly valuable for any robust web application developed by a professional custom website development team.

Key Use Cases:

  • Display region-specific offers in e-commerce platforms

  • Detect fraudulent signups by verifying geographic inconsistencies

  • Log visitor locations in admin dashboards for analytics

  • Automatically pre-fill country codes in registration or contact forms

Why This Matters for a Development Partner

In today’s digital landscape, clients expect highly personalized and intelligent user experiences. A website development company that incorporates IP-based geolocation features can build smarter, more intuitive applications. This not only enhances user engagement but also helps businesses achieve better conversion rates and customer satisfaction.

Final Words

Implementing IP-based geolocation in Laravel significantly improves both user experience and backend functionality. Using the stevebauman/location package offers a simple yet powerful way to add geolocation capabilities to your projects, making it an essential component of any comprehensive custom website development strategy.

Whether you are developing applications for global enterprises or local businesses, providing accurate geolocation data through your web development services can greatly enhance the usability, relevance, and overall performance of your products.

If you need assistance integrating this functionality into your Laravel application or want to explore advanced features like geofencing, consider partnering with a professional website development company for expert guidance and support.

Do you want help implementing this?

Get a summary via Google for

$0

Get Help Now! 
 
Get Help Now!

Tech Stack & Version

Frontend

  • HTML5
  • CSS3

Backend

  • PHP Version: ≥ 7.2
  • Laravel Version: ≥ 6.0

Deployment

  • Shared
  • VPS
  • Cloud
img

©2025Digittrix Infotech Private Limited , All rights reserved.