Get User Location using IP Address in Laravel 11

Websolutionstuff | May-01-2024 | Categories : Laravel

Hello developers! In this article, we'll see how to get user location using an IP address in laravel 11. Here, we'll learn about based on IP address get the user's current location in laravel 11. Also, we'll use stevebauman/location composer package.

This package retrieves a visitor's location from their IP address using various services. You will get country name, country code, region code, region name, city name, ZIP code, latitude, and longitude from the IP address.

Laravel 11 Get User Location using IP Address

Laravel 11 Get User Location using IP address

 

Step 1: Install Laravel 11 Application

 In this step, we'll install the laravel 11 application using the composer command.

composer create-project --prefer-dist laravel/laravel get_location_laravel_11

 

Step 2: Install stevebauman/location Package

 Then, we'll install stevebauman/location composer package using the following command.

composer require stevebauman/location

 

Step 3: Define a Route

 Now, we'll define routes into the web.php file.

routes/web.php

<?php

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


Route::get('ip_details', [UserController::class,'ip_details']);

 

Step 4: Create Controller

 Next, we'll create a controller and get the user location using $request->ip() function.

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 = '103.239.147.187'; // Static IP
        // $ip = $request->ip() //Dynamic IP address
        $data = Location::get($ip);
          
        return view('details', compact('data'));
    }
}

 

Step 5: Create Blade File

Then, we'll create details.blade.php file. 

resources/views/details.blade.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Get User Location using IP Address in Laravel 11 - techsolutionstuff.com</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  
<div class="container">
    <div class="card mt-5">
        <h3 class="card-header p-3">Get User Location using IP Address in Laravel 11 - techsolutionstuff.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>

 

Step 6: Run the Laravel 11 Application

Now, we'll run the laravel 11 application using the following command.

php artisan serve

 


You might also like :

Recommended Post
Featured Post
Laravel 8 REST API With Passport Authentication
Laravel 8 REST API With Passpo...

Hello Guys,  Today I will give you example of laravel 8 REST API with passport authentication. Also perform...

Read More

May-31-2021

How To Check Email Already Exist Or Not In Laravel
How To Check Email Already Exi...

In this article, we will show how to check whether the email already exists or not in laravel. Also, we will check...

Read More

Aug-07-2020

How To Add Watermark On Image In Laravel 10
How To Add Watermark On Image...

In this article, we will how to add a watermark to an image in laravel 10. Here we will learn about laravel 10 adding a...

Read More

Mar-31-2023

How To Get Multiple Checkbox Value In React JS
How To Get Multiple Checkbox V...

In this article, we will see how to get multiple checkbox values in react js. In react, js click on the submit butt...

Read More

Aug-29-2022