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

By Hardik Savani September 4, 2024 Category : Laravel

In this article, I will show you how to get current user location information from 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.

laravel 11 user current location ip

Step for Laravel 11 Get User Location from IP Address Example

  • Step 1: Install Laravel 11
  • 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 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 - 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 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

Preview:

i hope it can help you...

Shares