Laravel 9 Get Current User Location From IP

In this tutorial you will learn about the Laravel 9 Get Current User Location From IP and its application with practical example.

In this Laravel 9 Get Current User Location From IP tutorial I will show you how to get user location information using user IP address in laravel 9. In this tutorial you will learn to get country name, country code, city name, and address from user IP address in laravel 9. 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.

How to Get Current User Location in Laravel 9

In this tutorial you will learn how to get current user location with ip address using stevebauman location package. 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

Laravel 9 Get Current User Location From IP

  • Install Laravel 9
  • Connecting App to Database
  • Install stevebauman/location Package
  • Create Route
  • Create Controller By Artisan Command
  • Create Blade View
  • Run Development Server

Install Laravel 9

First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command

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.

Install stevebauman/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.

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:

Create 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

Create Controller By Artisan Command

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

Now, go to app\Http\Controllers and open UserController.php file. Then put the following code into your UserController.php file:

Create Blade View

In this step we will create blade view file. Go to resources/views directory and create user.blade.php. Then add the following code into it:

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 Laravel 9 Get Current User Location From IP and its application with practical example. I hope you will like this tutorial.