Determining User Location in Laravel 11 Using IP Addresses

Apr 20, 2024 | Laravel 11 Laravel


Hello Dev,

This article demonstrates how to retrieve current user location information from an IP address within a Laravel 11 application.

We'll leverage the stevebauman/location Composer package to extract various details like country name, country code, region code, region name, city name, ZIP code, latitude, and longitude based on the user's IP address. Follow these steps to implement this functionality in your Laravel app:

To get the current user location information from an IP address in a Laravel 11 application using the stevebauman/location package, follow these steps:

Step 1: Install Laravel 11

If you haven't already, create a new Laravel project:

composer create-project --prefer-dist laravel/laravel example-app
Read Also: integrating google charts in laravel 11: a comprehensive guide Step 2: Install stevebauman/location Package

Navigate to your project directory and install the stevebauman/location package:

composer require stevebauman/location
Step 3: Create a Route

Open your routes/web.php file and add a route for displaying the user location:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

Route::get('user', [UserController::class, 'index']);
Step 4: Create a Controller

Create a new controller named UserController:

php artisan make:controller UserController

In app/Http/Controllers/UserController.php, add the following code to fetch the user location information:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Stevebauman\Location\Facades\Location;
use Illuminate\View\View;

class UserController extends Controller
{
    public function index(Request $request): View
    {
        $ip = $request->ip(); // You can also use a static IP: $ip = '162.159.24.227';
        $currentUserInfo = Location::get($ip);

        return view('user', compact('currentUserInfo'));
    }
}
Read Also: ajax form validation with laravel 10 example Step 5: Create a Blade File

Create a new Blade file resources/views/user.blade.php to display the user location information:

<!DOCTYPE html>
<html>
<head>
    <title>Determining User Location in Laravel 11 Using IP Addresses -- ITErrorSolution.com</title>
</head>
<body>
    <h1>User Location Information</h1>
    <p>Country Name: {{ $currentUserInfo->countryName }}</p>
    <p>Country Code: {{ $currentUserInfo->countryCode }}</p>
    <p>Region Name: {{ $currentUserInfo->regionName }}</p>
    <p>Region Code: {{ $currentUserInfo->regionCode }}</p>
    <p>City Name: {{ $currentUserInfo->cityName }}</p>
    <p>ZIP Code: {{ $currentUserInfo->postalCode }}</p>
    <p>Latitude: {{ $currentUserInfo->latitude }}</p>
    <p>Longitude: {{ $currentUserInfo->longitude }}</p>
</body>
</html>
Step 6: Run the Application

Now, to run the Laravel application, please type the following command and press enter:

php artisan serve

Now, open your web browser and enter the provided URL to view the output of the application.

View Your Application
http://localhost:8000/user

This tutorial demonstrates how to use the stevebauman/location package to retrieve a visitor's location from their IP address in a Laravel 11 application, including extracting country name, country code, region code, region name, city name, ZIP code, latitude, and longitude

Thank you for your encouragement! If you have any questions or need further assistance, feel free to ask. I'm here to help!



I Am Not Take Properly Screen Shot, Can You Forgotten Me
Tags :
#Laravel 11
#Laravel
ItErrorSolution.com

ItErrorSolution.com

"Hey there! I'm a full-stack developer and proud owner of ItErrorSolution.com, based right here in India. I love nothing more than sharing handy tips and tricks with my fellow developers through easy-to-follow tutorials. When it comes to coding, I'm all about PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, CodeIgniter, and Bootstrap – been hooked on them forever! I'm all about putting in the work and staying committed. Ready to join me on this journey to coding?"