How to Get Location Information with IP Address in Laravel 8

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

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.

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.

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:

In response to the above question choose following provider from the options list using a number prefix:



You can also check on the config/location.php path.

Generate and Setting Up Controller

Now, lets create a controller named UserController using command given below –

Once the above command executed, it will create a controller file LocationController.php in app/Http/Controllers/ directory. Open the LocationController.php file and put the following code in it.

app/Http/Controllers/LocationController.php

Build 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

Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

In this tutorial we have learn about the How to Get Location Information with IP Address in Laravel 8 and its application with practical example. I hope you will like this tutorial.