Laravel 9 - Get user location

Touseef Afridi
29 Aug 24

Laravel 9 - Get user location

In this tutorial, we will discuss how to obtain the user's location in Laravel 9. Obtaining the user's location can help personalize content and services, such as displaying relevant local information or ads. It also enhances security by detecting and preventing fraudulent activities based on geographic location.


If you're a video person, feel free to skip the post and check out the video instead!


Step # 1 : Create fresh Laravel project.

Global Command : laravel new location
Or use
Non Global Command : composer create-project laravel/laravel --prefer-dist location

Step # 2 : Access the project.

Open a terminal (e.g., Git Bash) and navigate to your Laravel project's root folder.
Git Bash : cd c:xampp/htdocs/location
Run the Laravel Vite development server. Install the required dependencies and start the Vite server for front-end assets.
Command : npm install && npm run dev
Open a new Git Bash window or tab, and navigate to the same project directory to run further Laravel commands.

Step # 3 : Install Laravel location package.

Command : composer require stevebauman/location

Step # 4 : Publish the configuration (Optional)

Command : php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"
This will create a config/location.php file where you can tweak the configuration.

Step # 5 : Create a controller.

Command : php artisan make:controller LocationController
Update the LocationController with the following code.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Stevebauman\Location\Facades\Location;
class LocationController extends Controller
{
    public function index(Request $request)
    {
          // Define a static IP address for testing purpose.
             $ip = '9.9.9.9'; // Replace this with any valid IP for testing purpose.
             $location = Location::get($ip);
         // Get the user's IP address dynamically
        // $ip = $request->ip();
       // $location = Location::get($ip);
        return view('location', compact('location'));
    }
}

Step # 6 : Create a route.

Import LocationController class
use App\Http\Controllers\LocationController;
Create a route
Route::get('/location', [LocationController::class, 'index'])->name('location.show');

Step # 7 : Create a view.

In order to display the location data create a view named location.blade.php.
(resources/views/location.blade.php)
Paste the below code in location.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Location</title>
</head>
<body>
    <h1>User Location</h1>
    @if($location)
        <p><strong>Country:</strong> {{ $location->countryName }}</p>
        <p><strong>Region:</strong> {{ $location->regionName }}</p>
        <p><strong>City:</strong> {{ $location->cityName }}</p>
        <p><strong>Latitude:</strong> {{ $location->latitude }}</p>
        <p><strong>Longitude:</strong> {{ $location->longitude }}</p>
        <p><strong>IP Address:</strong> {{ $location->ip }}</p>
    @else
        <p>Location data is not available.</p>
    @endif
</body>
</html>

Step # 8 : It's time to test.

Run the Laravel server.
Command : php artisan serve
Access the URL /location, and you will receive details similar to the following:

User Location
Country: Some Country
Region: Some Region
City: Some City
Latitude: Some Latitude
Longitude: Some Longitude
IP Address: IP


Share this with friends!


"Give this post some love and slap that 💖 button as if it owes you money! 💸😄"
0

0 Comments

To engage in commentary, kindly proceed by logging in or registering