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' => [
'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 --

Step 3: Add Routes

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


use App\Http\Controllers\GeoLocationController;

[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:


namespace App\Http\Controllers;
use Illuminate\Http\Request;

class GeoLocationController extends Controller
    public function index()
        $ip = ''; //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


	<title>How to get current user location in laravel</title>
<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>

Step 6: Start Development Server

php artisan serve

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