Laravel get User Location, Country, City, Address from ip Address

laravel get user ip example; In this tutorial, you will learn how to get country, city and address, and current location using ip address in laravel app. Laravel get country list, state, zipcode, city all information of user using ip address is very easy using stevebauman location package.

Many times we need to fetch user information (country Name, Country Code, Region Code, Region Name, City Name, Zip Code, iso Code, postal Code, latitude, longitude, metro Code, metro Code) from IP address in Laravel app.

for getting user location information we are using “stevebauman/location” composer package in laravel app.

Step 1: Install the package

First, we need to install stevebauman/Location using the following command:

composer require stevebauman/Location

Step 2: Configure Config file

Now go to the config directory and open the app.php file where you need to register this package into Laravel 9 app by adding the following code to your app.php file:

'providers' => [
	Stevebauman\Location\LocationServiceProvider::class,
],
'aliases' => [
	'Location' => 'Stevebauman\Location\Facades\Location',
]

After that, execute the following command on the terminal to publish the config/location.php file:

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

Step 3: Add Routes

Now go to the routes folder and open the web.php file and add the following routes to your file:

routes/web.php

use App\Http\Controllers\GeoLocationController;

Route::get('get-address-from-ip',
[GeoLocationController::class, 'index']);

Step 4: Create Controller & Update Code

Next step, open your command prompt and execute the following command on the terminal to create a controller by an artisan:

php artisan make:controller GeoLocationController

After that, go to app\Http\Controllers and open the GeoLocationController.php file. Then update the following code into your GeoLocationController.php file:

app\Http\Controllers\GeoLocationController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class GeoLocationController extends Controller
{
    public function index()
    {
        $ip = '103.239.147.187'; //For static IP address get
        //$ip = request()->ip(); //Dynamic IP address get
        $data = \Location::get($ip);                
        return view('location-details',compact('data'));
    }
}

Step 5: Create Blade File

resources\views\location-details.blade.php

<html>
<head>
	<title>How to get current user location in laravel</title>
</head>
<body style="text-align: center;">
	<h1> How to get current user location in laravel</h1>
	<div style="border:1px solid black; margin-left: 300px; margin-right: 300px;">
	<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>

Step 6: Start Development Server

php artisan serve
http://localhost:8000/get-address-from-ip

So, In this tutorial, we learn how to get country, city, state, zip code, latitude, longitude, and address from IP address in Laravel 9 app. I hope you enjoy this.

Leave a Comment