Laravel 10 Get Current Location Details by IP Tutorial

Reading Time: 4 minutes
287 Views

Obtaining location information based on a user’s IP address can provide useful context for your online applications. Laravel 10 makes it easy to retrieve such information and improve user experiences. This tutorial will walk you through the stages of retrieving current location information by IP address using Laravel 10.

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.

Read More: Laravel 10 Auth with Livewire Jetstream Tutorial

The integration of location services in Laravel allows you to easily get a user’s approximate geographic information without requiring complicated preparations. In this article, we’ll look at the methods given by Laravel 10 for retrieving location information, talk about the underlying mechanics, and show how to use them.

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.

Location Composer Package Installation

Open project into terminal and run this composer command,

$ composer require stevebauman/Location

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

Create Location 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.

Read More: Laravel 10 API Testing Tool Package Tutorial

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 = '223.178.210.170'; 

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

To get your public IP, just go here.

Create Blade Template File

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

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

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Laravel 10 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 10 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. You 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

Read More: Laravel 10 How To Get Logged In User Data Tutorial

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

We hope this article helped you to learn about Laravel 10 Get Current Location Details by IP 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