Thursday, December 26, 2024
Google search engine
HomeLanguagesHow to get Country City Address from IP Address Laravel 9

How to get Country City Address from IP Address Laravel 9

Laravel 9 get country name, country code, city name, and address from user IP address example; In this tutorial, we will learn how to get country name, country code, city name, and address from user IP address in laravel 9 apps.

If you are working with laravel app and you may want to fetch user information like 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. So, this tutorial will help you fetch user info from IP it’s ip address.

How to get county name,city name, address from ip address in Laravel 9 Apps

Use the following steps to get country, city name, address from 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

In this step, open your terminal execute the following command 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

In this step, Go 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”

In this step, execute the following command on terminal 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

In this step, go to the routes folder and open web.php file, and add the following routes into your file:

routes/web.php

use App\Http\Controllers\GeoLocationController;

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

Step 5 – Create Controller By Command

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:

<?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

Now, execute the following command on the terminal 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

In this 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

If you have any questions or thoughts to share, use the comment form below to reach us.

RELATED ARTICLES

Most Popular

Recent Comments