ItSolutionStuff.com

How to Get User Location using IP Address in Laravel 12?

By Hardik Savani • April 15, 2025
Laravel

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.

laravel 12 user current location ip

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

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Generate Barcode in Laravel 12?

Read Now →

Laravel 12 Simple Pagination Example Tutorial

Read Now →

How to Generate App Key in Laravel 12?

Read Now →

Laravel 12 Chart using Highcharts JS Example

Read Now →

Laravel 12 Socialite Login with Google Account Example

Read Now →

Laravel 12 ChartJS Chart Example Tutorial

Read Now →

Laravel 12 REST API with Passport Authentication Tutorial

Read Now →

Laravel 12 One to Many Eloquent Relationship Tutorial

Read Now →

Laravel 12 Ajax CRUD Operation Tutorial

Read Now →

Laravel 12 Create Custom Error Page Example

Read Now →

Laravel 12 Generate Fake Data using Factory Tinker Example

Read Now →

Laravel 12 Clear Cache of Route, View, Config, Event Commands

Read Now →

Laravel 12 Image Upload Example Tutorial

Read Now →

How to Install PHP DOM Extension in Ubuntu 22.04?

Read Now →