Share this:

Today, We are going to learn How to get Country, City, Address from IP Address Laravel 9. For this tutorial we are going to use stevebauman/location composer package. From IP address you can get visitor’s Country, City, Stats and Address.

In this tutorial, We are creating a application which will get the visitor’s country name, country code, region name, region code, city name, zip code, latitude, longitude fetch all this information from IP address and display it into the view.

Steps on How to get Country City Address from IP Address Laravel 9

  • Step 1: Installing Fresh Laravel 9 Application
  • Step 2: Installing stevebauman/location Package
  • Step 3: Creating Routes
  • Step 4: Creating Controllers
  • Step 5: Creating Blade View Files
  • Step 6: Testing
  • Step 7: Conclusion

Also Read: Laravel Load More Data on Scroll with jQuery and Ajax

Step 1: Installing Fresh Laravel 9 Application

First, We are going to install fresh Laravel 9 Application. To install the latest Laravel 9 application run the following command in terminal.

composer create-project laravel/laravel blog

Note: “blog” is the our Laravel application name.

Step 2: Installing stevebauman/location Package

Now, we are going to install stevebauman/location composer Package for getting the current location of login user. To Install the package run the following command into terminal.

composer require stevebauman/location

Step 3: Creating Routes

After installing a stevebauman/location Package. Now we are going to create routes for our Laravel application.

Also Read: Upload Images to Server PHP Tutorial (2022)

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\UserController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('display-user', [UserController::class, 'index']);

Step 4: Creating Controllers

Now, We are going to create controller file. We are updating UserController file add the following code

app/Http/Controllers/UserController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Stevebauman\Location\Facades\Location;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        /* $ip = $request->ip(); Dynamic IP address */
        $ip = '48.188.144.248'; /* Static IP address */
        $currentUserInfo = Location::get($ip);
          
        return view('user', compact('currentUserInfo'));
    }
}

Please Note: We are using a static IP address of the testing example, because localhost didn’t fetch the records for localhost IP. You need to host online to work correctly!

Also Read: Create Livewire CRUD Application in Laravel 9 Example

Step 5: Creating Blade View Files

Now, We are going to display the information into the blade file. So, we are going to create user.blade.php file.

resources/views/user.blade.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>How to Get Current User Location with Laravel - LaravelTuts.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>How to Get Current User Location with Laravel - LaravelTuts.com</h1>
    <div class="card">
        <div class="card-body">
            @if($currentUserInfo)
                <h4>IP: {{ $currentUserInfo->ip }}</h4>
                <h4>Country Name: {{ $currentUserInfo->countryName }}</h4>
                <h4>Country Code: {{ $currentUserInfo->countryCode }}</h4>
                <h4>Region Code: {{ $currentUserInfo->regionCode }}</h4>
                <h4>Region Name: {{ $currentUserInfo->regionName }}</h4>
                <h4>City Name: {{ $currentUserInfo->cityName }}</h4>
                <h4>Zip Code: {{ $currentUserInfo->zipCode }}</h4>
                <h4>Latitude: {{ $currentUserInfo->latitude }}</h4>
                <h4>Longitude: {{ $currentUserInfo->longitude }}</h4>
            @endif
        </div>
    </div>
</div>
  
</body>
</html>

We had use bootstrap CDN to design our layout.

Step 6: Testing

So Now, Everything is done, We are going to test our Laravel Application. Run the following command to start the server:

php artisan serve

Now open the following URL into you browser address.

http://127.0.0.1:8000/display-user

Preview:

How to get Country, City, Address from IP Address Laravel 9
How to get Country, City, Address from IP Address Laravel 9

Step 7: Conclusion

Today, We had learn How to get Country, City, Address from IP Address Laravel 9. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: Automatically Backup Database Laravel 9

Share this:

Categorized in: