10-Apr-2023
.
Admin
Hello, friends today I will explain how to get the current user location in laravel 9 and other version in laravel 9, so you can just following to the my all step and learn to the get current user location in laravel 9.
In this tutorial, I am going to show you how we can track a user's IP address and can find user location information from the IP address.
For getting user location information I will use stevebauman/location package. Using this package you can get every information of the 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 the following command.
Step 1: Download Laravel
Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.
composer create-project laravel/laravel example-app
Step 2: 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 3: Configure Config file
After installing the package we have to setup the config file. So open the config/app.php file and add the service provider and alias.
config/app.php
'providers' => [
Stevebauman\Location\LocationServiceProvider::class,
],
'aliases' => [
'Location' => 'Stevebauman\Location\Facades\Location',
]
Step 4: Publish Optional Vendor
After setup config file to publish the configration file by just following command.
php artisan vendor:publish
Step 5: Add 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;
class UserController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
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 6: Add Route
Next step to add the route in your web.php file.
<?php
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('ip_details', [UserController::class, 'ip_details'])->name('ip_details');
Step 7: Add Blade File
The finaly last step is to create a blade file in details.blade.php and add the below Html code.
resources\views\details.blade.php
<html>
<head>
<title>How to get current user location in laravel 9</title>
</head>
<body style="text-align: center;">
<h1> How to get current user location in laravel 9</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>
Run Laravel App:
All steps have been done, now you have to type the given command and hit enter to run the laravel app:
php artisan serve
Now, you have to open web browser, type the given URL and view the app output:
http://localhost:8000/ip_details
#Laravel 9