DEV Community

Cover image for Laravel 8 Get Country, City & Address From IP Address Tutorial
AbdELrahman Mohamed
AbdELrahman Mohamed

Posted on

Laravel 8 Get Country, City & Address From IP Address Tutorial

HOO-ah , welcome in this simple laravel 8 tutorial !


How to get country name from IP address in Laravel 8. In this tutorial, you will learn how to get country, city and address from ip address in laravel 8 app.

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

In this tutorial, we will fetch IP Address using “stevebauman/location” composer package in Laravel 8 app.

Note that, Using this package”stevebauman/location”, you can also get country Name, country Code, region Code, region Name, city Name, zip Code, iso Code, postal Code, latitude, longitude, metro Code, metro Code.


How to get location(county,city address) information from ip address in Laravel

  • Step 1 – Install Laravel 8 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 8 App

In this step, open your terminal execute the following command to install laravel 8 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 following to connecting app to 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 8 app:

composer require stevebauman/location

Then, Go to config directory and open app.php file. And register this package into laravel 8 app by adding the following code into your app.php file:

'providers' => [

....

Stevebauman\Location\LocationServiceProvider::class,
Enter fullscreen mode Exit fullscreen mode

],

'aliases' => [

....

'Location' => Stevebauman\Location\Facades\Location::class,
Enter fullscreen mode Exit fullscreen mode

]

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

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

Step 4 – Add Routes

In this step, go to 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 terminal to create a controller by an artisan:

php artisan make:controller GeoLocationController

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

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Stevebauman\Location\Facades\Location;

class GeoLocationController extends Controller
{

public function index(Request $request)
{

      $data = Location::get();
       dd($data);

}
}


Step 6 – Start 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 8 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 8 app.

Top comments (1)

Collapse
 
aymenalhattami profile image
Ayman Alhattami

It does not give the specific data