Hi Developers,
Lets see How To Get User Location In Laravel by using the Stevebauman/Location package. In this tutorial we will see How To Get User Location In Laravel. In some of the projects we need to access the users location to make the analytics based on the location of the users. Its a complex process of accessing the location of the user , Well it was made easy by now using the Laravel request()->ip() method and also the Stevebauman/Location package to access the users location. By using these both initially we will get the IP Address of the user and then by using the Stevebauman/Location package we can get the location of the user.
Discover Laravel's user location effortlessly with ITSolutionsGuides tutorial. Learn the steps to retrieve current user location seamlessly.
Fisrt lets install Stevebauman/Location package to retrieve user's location in our Laravel Project
composer require stevebauman/location
We can add the installed package by running this following command,
php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"
or we can add service provider and aliase manually in the config/app.php
'providers' => [
Stevebauman\Location\LocationServiceProvider::class,
],
'aliases' => [
'Location' => 'Stevebauman\Location\Facades\Location',
],
Lets use the installed package in the controller function to Acess the location,
Now create controller on this path app\Http\Controllers\UserController.php and add below command.
In this code initially we get the users location by using the request()->ip() after getting the users IP Address, We get the location of the user using the IP address.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function userDetails()
{
$ip = request()->ip(); //Dynamic IP address get
$data = \Location::get($ip);
return view('details',compact('data'));
}
}
In this add routes in web.php file.
Route::get('user_details', [UserController ::class, 'userDetails']);
Lets create the blade file to display the users Location details.
<html>
<head>
<title>How To Get User Location In Laravel</title>
</head>
<body style="text-align: center;">
<h1> How To Get User Location In Laravel</h1>
<div class="card p-5 m-5">
<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>
We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.
Be the first to know about releases and tutorial news and solutions.
We care about your data in our privacy policy.
ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.
Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides