Laravel 9 Get Locations By IP Address

Sovary July 5, 2022 803
3 minutes read

Laravel 9 get locations country name, country code, city name, and address from user IP; In this article, we are going to explain how to get country info city name, postal code and address latitude, longitude from user IP address in Laravel 9 with package tevebauman/location.

Laravel 9 Get Locations By IP Address

Following step below to approche implement how to get location in Laravel

Step 1 - Install Laravel 9

You have to installed Laravel framework. Read this to know how to install Laravel 9 project with database configuration.

Step 2 - Install Package

We need install a third party package called tevebauman/location to enable Class to retrieve data related to Ip Address. Run following command in your root project

composer require stevebauman/location
In case, your Laravel version greater than 5.5+ you can skip the registration of the service provider below.

Open file app -> config.php

'providers' => [
    ....

    Stevebauman\Location\LocationServiceProvider::class
],

'aliases' => [
    ....

    'Location' => Stevebauman\Location\Facades\Location::class,
],

Run command below to publish config file.

php artisan vendor:publish

Step 3 - Create Controller

Next step, open you command prompt and execute below command to generate a controller file and Class named LocationController

php artisan make:controller LocationController

Open file app -> Http -> Controllers -> LocationController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Location;

class LocationController extends Controller
{
    public function index(Request $request)
    {
      // For dynamic Ip testing
      //$ip = $request->ip();

      // For static Ip testing
      $ip = "36.68.17.155";
      $loc = Location::get($ip);
      return view('index',["location"=>$loc]);
    }
}

Step 4 - Adding Route

We are going to adding route to point URL and Controller, so open the web.php file and add the following code:

Open file routes -> web.php

<?php

use App\Http\Controllers\LocationController;

....

Route::get('/', [LocationController ::class, 'index']);

 

Step 5 - Create View (Blade)

We need a web page to display data which is render in HTML file. We are going to create a blade file call index.blade.php

Create file resources -> views -> index.blade.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Get Location via Ip Address - CamboTutorial.com</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  
<div class="container">
    <h1>Get Location via Ip Address - CamboTutorial.com</h1>
    <div class="card">
        <div class="card-header">
        <h3>Your IP: {{ $location->ip }}</h3>
        </div>
        <div class="card-body">
            <ul class="list-group">
            <li class="list-group-item"><h5>Country Name: {{ $location->countryName }}</h5> </li>
            <li class="list-group-item"><h5>Country Code: {{ $location->countryCode }}</h5></li>
            <li class="list-group-item"><h5>Region Code: {{ $location->regionCode }}</h5></li>
            <li class="list-group-item"><h5>Region Name: {{ $location->regionName }}</h5></li>
            <li class="list-group-item"><h5>Area Code: {{ $location->areaCode }}</h5></li>
            <li class="list-group-item"><h5>City Name: {{ $location->cityName }}</h5></li>
            <li class="list-group-item"><h5>Zip Code: {{ $location->zipCode }}</h5></li>
            <li class="list-group-item"><h5>Latitude: {{ $location->latitude }}</h5></li>
            <li class="list-group-item"><h5>Longitude: {{ $location->longitude }}</h5></li>
            <li class="list-group-item"><h5>City Name: {{ $location->cityName }}</h5></li>
            <li class="list-group-item"><h5>Time Zone: {{ $location->timezone }}</h5></li>
            </ul>
        </div>
    </div>
</div>
  
</body>
</html>

Step 6 - Run Laravel Server

We are going start the Laravel server with following command, so we can test and launch the app via browser URL http://localhost:8000/

php artisan serve

laravel-9-get-geo-location-country-via-ip-address

Hope this tutorial help you to implement your project. Have a nice day!

You might Also Like:

Laravel  PHP  Laravel 9 
Author

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him