Laravel 9 Get Country, City Name & Address From IP Address

Laravel 9 get country name, country code, city name, and address from user IP address; Through this tutorial, i am going to show you how to get country name, country code, city name, postal code and address from user IP address in laravel 9 apps.

Laravel 9 Get Country, City Name & Address From IP Address

Follow the below given steps to get country name, country code, city name, postal code and address from user IP address in laravel 9 apps:

  • Step 1 – Install Laravel 9 App
  • Step 2 – Connecting App to Database
  • Step 3 – Install “stevebauman/location”
  • Step 4 – Add Routes
  • Step 5 – Create Controller By Command
  • Step 6 – Start Development Server

Step 1 – Install Laravel 9 App

Run the following command on command prompt to install laravel 9 app into your local system or live server:

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

Step 2 – Connecting App to Database

Visit to your project root directory, find .env file, and add database details as follows to connect the app to the database:

 DB_CONNECTION=mysql 
 DB_HOST=127.0.0.1 
 DB_PORT=3306 
 DB_DATABASE=here your database name here
 DB_USERNAME=here database username here
 DB_PASSWORD=here database password here

Step 3 – Install “stevebauman/location”

Run the following command on command prompt to install stevebauman/location package in laravel app:

composer require stevebauman/location

Then, Go to the config directory and open the app.php file. And register this package into laravel app by adding the following code into 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 config/location.php file:

php artisan vendor:publish

Step 4 – Add Routes

Visit the routes folder and open web.php file, and add the following routes into your file:

use App\Http\Controllers\GeoLocationController;
Route::get('get-address-from-ip', [GeoLocationController::class, 'index']);

Step 5 – Create Controller By Command

Run the following command on the command prompt 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:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Location;
class GeoLocationController extends Controller
{
   
    public function index(Request $request)
    {
            $ip = $request->ip();
            $data = \Location::get($ip);
            dd($data);
    }
}

Step 6 – Start Development Server

Run the following command on the command prompt to start the development server:

php artisan serve

Then open your browser and hit the following URL on it:

http://localhost:8000/get-address-from-ip

Conclusion

Laravel 9 get country, city, state, zip code, latitude, longitude and address from ip address tutorial, you have learned how to fetch country, city, state, zip code, latitude, longitude and address from ip address in laravel 9 app.

Recommended Laravel Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.