Get user location based on IP address in Laravel

Display User Location Information Based on IP address Laravel allows you to detect where your users are connecting from and retrieve detailed location data. By using the stevebauman/location package, you can easily get information such as country, city, latitude, and longitude, making it simple to enhance user experiences or tailor content based on location.

By using this package you can get below information:

  • Country Name
  • Country Code
  • Region Name
  • Region Code
  • City Name
  • Zip Code
  • ISO Code
  • Postal Code
  • Metro Code
  • Latitude and Longitude

Install stevebauman/location Package

For Installation, you just need to enter the below command into the terminal.

composer require stevebauman/location

Here, we will use this package using import so we don’t need to register into our application.

Create Location Controller

Let’s create a controller for getting location data and display it using view. First thing you need to enter the below command to create a controller :

php artisan make:controller LocationController
<?php

namespace App\Http\Controllers;

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

class LocationController extends Controller
{
    public function getLocation(Request $request)
    {
        // $ip = $request->ip(); /*For Actual IP address on Live Server */
        $ip = '66.150.71.232'; /* Static IP address */

        $locationInfo = Location::get($ip);
        return view('location-info', compact('locationInfo'))
    }
}

Here, the getLocation() method will get the user’s IP address from the request and get location data. At last, Pass that location data to view./p>

If you are running your application on a live server then you need to use $request->ip() method to get information. Here, an IP address is defined statically for testing.

Create View

Now, you have location data so you will need a view to display information to a user. So create a view in the views directory and make the below changes.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Display User Information Based On IP address - Codewolfy.com</title>
</head>
<body>
    <div class="container">
        <div class="card">
            <div class="card-body">
                @if($locationInfo)
                    <h4>IP Address: {{ $locationInfo->ip }}</h4>
                    <h4>Country : {{ $locationInfo->countryName }}</h4>
                    <h4>Country Code: {{ $locationInfo->countryCode }}</h4>
                    <h4>Region Code: {{ $locationInfo->regionCode }}</h4>
                    <h4>Region Name: {{ $locationInfo->regionName }}</h4>
                    <h4>City Name: {{ $locationInfo->cityName }}</h4>
                    <h4>Zip Code: {{ $locationInfo->zipCode }}</h4>
                    <h4>Latitude: {{ $locationInfo->latitude }}</h4>
                    <h4>Longitude: {{ $locationInfo->longitude }}</h4>
                    <h4>Area Code: {{ $locationInfo->areaCode }}</h4>
                @else
                    <h3>Something went wrong. Please try again</h3>
                @endif
            </div>
        </div>
    </div>
</body>
</html>

Add Route

In the Last Step, you need to add a route. open the route file and make the below changes :

<?php

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

Route::get('/', function () {
    return view('welcome');
});

Route::get('get-my-location', [LocationController::class, 'getLocation']);

Testing Our Application / Functionality

Now we are ready to test our functionality. open the terminal and run the below command :

php artisan serve

Open this URL in a browser and it will show location information.

http://127.0.0.1:8000/get-my-location

Whenever the user hits this URL it will display location data and in addition, if location data is now found then it will display an error message. Generally, errors arise when you are not running the application into production and trying to get IP address using $request->ip().

Conclusion

By integrating the stevebauman/location package into your Laravel application, you can efficiently retrieve and display user location information based on their IP address. This functionality enhances user experience by enabling features such as personalized content delivery, regional analytics, and targeted marketing strategies.

For adding more context to user requests, you can also explore Restrict Website Access Based on IP Address In Laravel to see how controlling access by IP complements displaying user location information in your application.