Laravel 9 Get Country, City, and Address Information from IP Address effortlessly Step by Step with Example [Full Tutorial]

Laravel 9 Get Country, City, and Address Information from IP Address effortlessly Step by Step with Example

Laravel 9 Get Country, City, and Address Information from IP Address effortlessly Step by Step with Example: This tutorial will give you an example of how to use the stevebauman/location package in Laravel 9, combined with a simple step-by-step explanation, to fetch the country name, city name, and address details associated with an IP address. By following along, you’ll learn how to get location information including the country, city, and state from an IP address when using Laravel 9.

In this Laravel tutorial, we will utilize the stevebauman/location composer package to retrieve the current user’s location in a Laravel app. We will extract information such as the country name, country code, region code, region name, city name, zip code, latitude, and longitude from the user’s IP address. By following the steps below, you will be able to achieve the desired outcome.

Step 1: Install Laravel 9 App

In this step, You can create a Laravel 9 new project. So open the terminal and write the command.

composer create-project --prefer-dist laravel/laravel userLocation

Step 2: Install stevebauman/location Composer Package

In this step we need to install stevebauman/location composer package via the Composer package manager, so one your terminal and fire the bellow command:

composer require stevebauman/location

Step 3: Create Routes

In this step, we will create a route for getting requests. So, let’s add a new route to that file.


    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('user-information', [UserController::class, 'get_user_location']);


Step 4: Create User Controller

in the next step, we have to create a new controller as UserController and write both methods on it like as below, So let’s create the controller below command:

php artisan make:controller UserController

 namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 use Stevebauman\Location\Facades\Location;
 class Userontroller extends Controller
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function index(Request $request)
          /*$ip = $request->ip(); Dynamic IP address*/
          $ip = ''; /* Static IP address*/
          $currentUserInfo =  Location::get($ip);

          return view('user', compact('currentUserInfo'));     

Read Also: Stripe Payment Gateway Integration in Laravel 9 with step-by-step Example [Full Tutorial]

Step 5: Create Blade File


Run Laravel Project

<!DOCTYPE html>
    <meta charset="utf-8">
    <link href="" rel="stylesheet">
    <div class="container">
        <h1>How to Get Current User Location with Laravel -</h1>
		<div class="card">
		<div class="card-body">
		<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>


All the necessary steps have been completed. Now, please input the following command and press enter to execute the Laravel application:

php artisan serve

Now, open your web browser, enter the given URL, and observe the output of the app.



Any Kind Of Help Or Needs So Contact Me:
Facebook Group: ]


About Post Author

, , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *