In this tutorial you will learn about the How to Get Location Information with IP Address in Laravel 8 and its application with practical example.
In this How to Get Location Information with IP Address in Laravel 8 tutorial I will show you how to get user location information using IP address in laravel 8. In this tutorial you will learn to get user’s country name, country code, city name, and address from user IP address in laravel 8. In this example I will help you to fetch user location data from 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.
The stevebauman/location laravel package is used to get users location data by their IP Address. This helps in retrieving user’s location information from IP address. The location library helps in retrieving following location information:
- Country name and code
- Region name and code
- City name
- Zipcode
- ISO code
- Postal code
- Latitude and longitude
- Metro code
How to Get Location Information with IP Address in Laravel 8
In this tutorial you will learn how to get current user location with ip address using stevebauman location package. Please follow the instruction given below:
Create Laravel Project
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project laravel/laravel laravel-get-location-information-example --prefer-dist |
Make Database Connection
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=databse DB_USERNAME=root DB_PASSWORD= |
Install Location Package
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 |
Register Location Package
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 16 17 |
'providers' => [ .... .... .... Stevebauman\Location\LocationServiceProvider::class, ], 'aliases' => [ .... .... .... 'Location' => 'Stevebauman\Location\Facades\Location', ] |
In response to the above question choose following provider from the options list using a number prefix: