Laravel 9 Get Current User Location Using IP Address
Tags: laravel9, php, ipaddress, location
Published: 2022-12-09 05:30:00

In this article, we will see laravel 9 get the current user location using an IP address. 

Many times we are required to find the current location of users for any purpose. 

So, here we will use stevebauman/location laravel package. Using this package you can get much information about the utilizer like postal code, zip code, region denomination, state name longitude, country denomination, latitude, iso code, etc. Detect a user's location by their IP Address.

So, let's see how to get the current user location in laravel 9, get the location in laravel 9, and get the location using an IP address in laravel 9.

> Step 1: Install Laravel 9

In this step, we will install laravel 9 to get the user location using the following command.

```
composer create-project --prefer-dist laravel/laravel laravel_9_get_user_location
``` 

---

**_[Read Also: Laravel 9 Dynamic Line Chart Example](https://techsolutionstuff.com/post/laravel-9-dynamic-line-chart-example)_**

--- 

> Step 2: Install stevebauman/location Package

In this step, we will install the stevebauman/location package using the following command.

```
composer require stevebauman/location
``` 

> Step 3: Add Service Provider And Aliase

Now, we will add the service provider in config/app.php file.

If you're using Laravel 5.5 or above, you can skip the registration of the service provider, as it is registered automatically.

```
'providers' => [
	
	Stevebauman\Location\LocationServiceProvider::class,
],

'aliases' => [
	
	'Location' => 'Stevebauman\Location\Facades\Location',
],
``` 

> Step 4: Create Controller

Now, create a UserController.php file. and add the below code in that file.

app\Http\Controllers\UserController.php

```

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
    public function ip_details()
    {
        $ip = '103.239.147.187'; //For static IP address get
        //$ip = request()->ip(); //Dynamic IP address get
        $data = \Location::get($ip);                
        return view('details',compact('data'));
    }
}
``` 

> Step 5: Add Route

Now, we will add a route in the web.php file
routes\web.php

```
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;


Route::get('ip_details', [UserController::class,'ip_details']);
``` 

---

**_[Read Also: Laravel 9 Multiple Image Upload Example](https://techsolutionstuff.com/post/laravel-9-multiple-image-upload-example)_**
 
---

> Step 6: Create Blade File

In this step, we will create a details.blade.php file to get current user location details.

resources\views\details.blade.php

```
<html>
<head>
	<title>Laravel 9 Get Current User Location Using IP Address - Techsolutionstuff </title>
</head>
<body style="text-align: center;">
	<h1> Laravel 9 Get Current User Location Using IP Address - Techsolutionstuff </h1>
	<div style="border:1px solid black; margin-left: 300px; margin-right: 300px;">
	<h3>IP: {{ $data->ip }}</h3>
	<h3>Country Name: {{ $data->countryName }}</h3>
	<h3>Country Code: {{ $data->countryCode }}</h3>
	<h3>Region Code: {{ $data->regionCode }}</h3>
	<h3>Region Name: {{ $data->regionName }}</h3>
	<h3>City Name: {{ $data->cityName }}</h3>
	<h3>Zipcode: {{ $data->zipCode }}</h3>
	<h3>Latitude: {{ $data->latitude }}</h3>
	<h3>Longitude: {{ $data->longitude }}</h3>
	</div>
</body>
</html>
```

Output:

![laravel_9_get_current_user_location_using_ip_address](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbyjm1u60d8d9pgh6yqd.png)
 
---

You might also like:

- **_[Read Also: How To Connect Multiple Database In Laravel 9](https://techsolutionstuff.com/post/how-to-connect-multiple-database-in-laravel-9)_**

- **_[Read Also: Laravel 9 Import Export CSV And EXCEL File](https://techsolutionstuff.com/post/laravel-9-import-export-csv-and-excel-file)_**

- **_[Read Also: Datatable Filter With Dropdown In Laravel 9](https://techsolutionstuff.com/post/datatable-filter-with-dropdown-in-laravel-9)_**