In this tutorial you will learn about the Laravel 8 Get Country, City Name & Address From IP Address Example and its application with practical example.
In this Laravel 8 get country name, country code, city name, and address from user IP address example tutorial I will show you how to get country name, country code, city name, and address from user IP address in laravel 8. In this tutorial you will learn to get country name, country code, city name, and address from user IP address in laravel 8. When you working on web development project you come to situation where you may want to fetch user information such as 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. In this example I will help you to fetch user info from IP their ip address. We will be using stevebauman/location package in this example to fetch country name, country code, city name, and address from IP address.
Laravel 8 Get Country, City Name & Address From IP Address Example
In this step by step tutorial I will demonstrate you with example to fetch country name, country code, city name, and address from IP address. Please follow the instruction given below:
- 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
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2 – Connecting App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
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, we will install stevebauman/location Package via the composer dependency manager. Use the following command to install stevebauman/location Package.
1 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
'providers' => [ .... Stevebauman\Location\LocationServiceProvider::class, ], 'aliases' => [ .... 'Location' => 'Stevebauman\Location\Facades\Location', ] |
Now you need to run the following command on terminal to publish config/location.php file:
1 |
php artisan vendor:publish |
Step 4 – Add Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 |
use App\Http\Controllers\GeoLocationController; Route::get('get-address-from-ip', [GeoLocationController::class, 'index']); |
Step 5 – Create Controller By Command
Now, lets create a controller named GeoLocationController using command given below –
1 |
php artisan make:controller GeoLocationController |
Now, go to app\Http\Controllers
and open GeoLocationController.php file. Then put the following code into your GeoLocationController.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class GeoLocationController extends Controller { public function index(Request $request) { $ip = $request->ip(); $data = \Location::get($ip); dd($data); } } |
Step 6 – Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/get-address-from-ip |