How To Get Current User Location using IP Address in Laravel 8
Tags: laravel, howto, php
Published: 2022-03-28 03:46:10

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-get-current-user-location-using-ip-address-in-laravel-8

[![Advanced Laravel SAAS Starter Kit with CRUD Generator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wyjyev8tz3r8vi2w3on1.png)](https://1.envato.market/5g5QAn)

[Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!](https://1.envato.market/5g5QAn)

In this post, I'm sharing **how to get the current user location using the IP address in Laravel 8.** Usually building a project we need to determine where our users came from so that we have their location so that we have an idea about it. Like if we need to scale our server or business in that location because we have many users from there.

![how-to-get-current-user-location-using-ip-address-in-laravel-8](https://ecn-storage.s3.us-west-2.amazonaws.com/articles/how-to-get-current-user-location-using-ip-address-in-laravel-8-JFjDd2VpqLx980.webp?ctm=1630673704)

In this example, I'm using **stevebauman/location package to** make easier our task.


Here is the step by step below:


I assume that you have basics already about Laravel and you have your local project already. 

First, we need to install the package. Run the following command below:


```php
composer require stevebauman/location
```

Second, after installing the package. Let's create our controller. Run the following command below:

```php
php artisan make:controller UserLocationController
```

Third, create our route. See below sample:

```php
Route::get('/user-location', 'UserLocationController@index')->name('user.location.index');
```

Then check our controller code below:

```php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Stevebauman\Location\Facades\Location;

class UserLocationController extends Controller
{
    public function index() 
    {
        // Call this dynamically or use 
        // $_SERVER['REMOTE_ADDR'] but only works in server not in local
        $yourUserIpAddress = '66.102.0.0';

        $location = Location::get($yourUserIpAddress);

        return view('user-location.index', compact('location'));
    }
}
```

As you can see I imported the **Location** class and we call it to our **index()** method.

Then next let's view it.

```php
@extends('layouts.app-master')

@section('content')
    <div class="bg-light p-5 rounded">
        <h1>How To Get Current User Location using IP Address in Laravel 8</h1>
        
        <div class="mt-5">
            <p><b>Country:</b> {{ $location->countryName }}</p>
            <p><b>Country Code:</b> {{ $location->countryCode }}</p>
            <p><b>Region Code:</b> {{ $location->regionCode }}</p>
            <p><b>Region Name:</b> {{ $location->regionName }}</p>
            <p><b>City Name:</b> {{ $location->cityName }}</p>
            <p><b>Zip Code:</b> {{ $location->zipCode }}</p>
            <p><b>Latitude:</b> {{ $location->latitude }}</p>
            <p><b>Longitude:</b> {{ $location->longitude }}</p>
            <p><b>Area Code:</b> {{ $location->areaCode }}</p>
        </div>
    </div>
@endsection
```

That's it you have the basics on how to do it. I hope it helps. For more info about the package, we are using kindly check [here](https://github.com/stevebauman/location/).

[![Advanced Laravel SAAS Starter Kit with CRUD Generator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wyjyev8tz3r8vi2w3on1.png)](https://1.envato.market/5g5QAn)

[Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!](https://1.envato.market/5g5QAn)

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-get-current-user-location-using-ip-address-in-laravel-8 if you want to download this code.

Happy coding :)