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

By Hardik Savani April 18, 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.


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:


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 = '';
        $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.


<!DOCTYPE html>
    <meta charset="utf-8">
    <title>How to Get Current User Location with Laravel -</title>
    <link href="" rel="stylesheet">
<div class="container">
    <div class="card mt-5">
        <h3 class="card-header p-3">How to Get Current User Location with Laravel 11?-</h3>
        <div class="card-body">
                <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>

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:



i hope it can help you...