Laravel 9 How To Get Current Location Details Tutorial

Reading Time: 4 minutes
1,205 Views

Inside this article we will see the concept i.e Laravel 9 How To get current location details by User’s IP address. Article contains classified information about getting user’s location detailed address by using their IP address.

An IP address is a unique address that identifies a device on the internet or a local network. IP stands for “Internet Protocol,” which is the set of rules governing the format of data sent via the internet or local network.

We will use a laravel composer package to get the location details. Details will be like – Country name, Country code, City name, Zip code, Latitude, etc.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Install Composer Package

Open project into terminal and run this composer command to install this Location package.

$ composer require stevebauman/Location

It will install all needed package files into /vendor folder.

Create Controller

Go to terminal and run this artisan command to create controller file.

$ php artisan make:controller LocationController

It will create LocationController.php inside /app/Http/Controllers folder.

Open file LocationController.php and write this complete code into it.

<?php

namespace App\Http\Controllers;

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

class LocationController extends Controller
{
    public function ip_details()
    {
        // Put your IP address
        $ip = '101.0.49.139'; 

        $data = Location::get($ip);
        
        return view('userinfo', compact('data'));
    }
}

Create Blade Template File

Go to /resources/views folder and create a file userinfo.blade.php.

Open userinfo.blade.php and write this complete into it,

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Laravel 9 How To Get Current Location Details Tutorial</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  
<div class="container" style="margin-top: 20px;">
    <h3>Laravel 9 How To Get Current Location Details Tutorial</h3>
    <div class="card">
        <div class="card-body">
            @if($data)
                <h4>IP: {{ $data->ip }}</h4>
                <h4>Country Name: {{ $data->countryName }}</h4>
                <h4>Country Code: {{ $data->countryCode }}</h4>
                <h4>Region Code: {{ $data->regionCode }}</h4>
                <h4>Region Name: {{ $data->regionName }}</h4>
                <h4>City Name: {{ $data->cityName }}</h4>
                <h4>Zip Code: {{ $data->zipCode }}</h4>
                <h4>Latitude: {{ $data->latitude }}</h4>
                <h4>Longitude: {{ $data->longitude }}</h4>
            @endif
        </div>
    </div>
</div>
  
</body>
</html>

Add Route

Open web.php file from /routes folder. We need to add a route into it.

//...

use App\Http\Controllers\LocationController;

Route::get('ip-details', [LocationController::class, 'ip_details'])->name('ip_details');

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL: http://127.0.0.1:8000/ip-details

We hope this article helped you to learn about Laravel 9 How To Get Current Location Details Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more