How to Get Current User Location in Laravel 8?

10-Apr-2023

.

Admin

How to Get Current User Location in Laravel 8?

Hello friends today i will explained how to get current user location in laravel 8 and other version in laravel ,so you can just following to the my all step and learn to the get current user location in laravel.

In this tutorial i am going to show you how we can track user ip address and can find user location information from ip address.

For getting user location information i will use stevebauman/location package. Using this package you can get every information of user like country name, region name, state name longitude, latitude, zip code, iso code, postal code etc.

So let's start. we have to install stevebauman/location package. In just following command.

Step 1 : Install package


Your project is required to the composer require stevebauman/Location this package so following command and install package.

composer require stevebauman/Location

Step 2 : Configure Config file

After installing package we have to setup config file. So open config/app.php file and add service provider and alias.

'providers' => [

Stevebauman\Location\LocationServiceProvider::class,

],

'aliases' => [

'Location' => 'Stevebauman\Location\Facades\Location',

]

Step 3 : Publish Optional Vendor

After setup config file to publish the configration file by just following command.

php artisan vendor:publish

Step 4 : Create Controller

Then next to create a UserController in the following command

php artisan make:controller UserController

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

Next step to add the route in your web.php file.

use App\Http\Controllers\UserController;

Route::get('ip_details', [UserController::class, 'ip_details'])->name('ip_details');

Step 6 : Create Blade File

Then finaly last step to create a blade file in details.blade.php and add below html code.

resources\views\details.blade.php

<html>

<head>

<title>How to get current user location in laravel</title>

</head>

<body style="text-align: center;">

<h1> How to get current user location in laravel</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>

So, finally we are done with our code we can get below output.

Run your project in following command:

php artisan serve

Browser url run : http://localhost:8000/ip_details

#Laravel 8