Laravel 8 Get Country, City Name & Address From IP Address Example

Laravel 8 Get Country, City Name & Address From IP Address Example

Laravel 8 get country name, country code, city name, and address from user IP address example; In this tutorial, you will learn how to get country name, country code, city name, and address from user IP address in laravel 8 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 8 app. So, this tutorial will help you fetch user info from IP it’s ip address.

So this tutorial will use IP Address to get country name, country code, city name, and address from IP address with”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 county name,city name, address 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,

],

'aliases' => [

	....

	'Location' => 'Stevebauman\Location\Facades\Location',

]

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

php artisan vendor:publish

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 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 terminal to 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.

Recommended Laravel Posts

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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