How To Get Current Location Details in Laravel 8 Tutorial

Share this Article
Reading Time: 4 minutes
16 Views

Inside this article we will see the complete concept to get current location details in laravel 8. It will be super easy to understand and very few simple steps to integrate into laravel application.

We will use a composer package which helps us to get the all the details country, zip code, latitude, longitude etc.

This article is not laravel 8 specific even you are free to use in any of your version.

Let’s get started.


Installation of Laravel 8 Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Click here to Install Composer ? Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel 8 application into system. To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel 8 already installed at 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.


Configure Package in Application

Open app.php file from /config folder.

Search for providers, add this line to into array

//...

Stevebauman\Location\LocationServiceProvider::class,

Search for aliases, add this line to into array

//...

'Location' => 'Stevebauman\Location\Facades\Location',

Publish Configuration Files

Open project in terminal and run this command to publish the files.

$ php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"

Create Controller

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

$ php artisan make:controller LocationController

It will create LocationController.php at /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 Location;

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

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

Create Blade Layout File

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

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

<html>

<head>
    <title>Current Location Details in Laravel 8</title>
    <style>
        .box {
            margin: auto;
            width: 50%;
            border: 3px dashed green;
            padding: 10px;
            text-align: center;
        }

    </style>
</head>

<body style="text-align: center;">
    <h1>Current Location Details in Laravel 8</h1>
    <div class="box">
        <h3>IP: {{ $data->ip }}</h3>
        <h3>Country Name: {{ $data->countryName }}</h3>
        <h3>Country Code: {{ $data->countryCode }}</h3>
        <h3>Region Code: {{ $data->regionCode }}</h3>
        <h3>Region Name: {{ $data->regionName }}</h3>
        <h3>City Name: {{ $data->cityName }}</h3>
        <h3>Zipcode: {{ $data->zipCode }}</h3>
        <h3>Latitude: {{ $data->latitude }}</h3>
        <h3>Longitude: {{ $data->longitude }}</h3>
    </div>
</body>

</html>

Create Routes

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

# Add this to header
use App\Http\Controllers\LocationController;

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

Application Testing

Open project to terminal and type the command 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 How To Get Current Location Details in Laravel 8 Tutorial in a very detailed way.

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.

Find More on Laravel 8 Articles here

Leave a Comment