In this example, I will show you how to get the current user location in laravel, Many times we are required to find the current location of users for any purpose. So, here I am using stevebauman/location laravel package, Using this package you can get information about the utilizer like postal code, zip code, region denomination, state name longitude, country denomination, latitude, iso code, etc.
So, let's see how to get current user location in laravel, how to get current user location details in laravel, get the location in laravel, get location using the IP address in laravel, and how to get current user location using the IP address in laravel.
So, let's start and follow the below step one by one.
Step 1 : Install Laravel
Type the following command in the terminal to create a new project in your system.
composer create-project --prefer-dist laravel/laravel get_location
Step 2 : Install stevebauman/location Package In Your Application
After installation of the project, you need to install stevebauman/location Package
composer require stevebauman/location
Step 3 : Add Service Provider And Aliase
After package installation, we need to add service provider and aliase in config/app.php.
'providers' => [
Stevebauman\Location\LocationServiceProvider::class,
],
'aliases' => [
'Location' => 'Stevebauman\Location\Facades\Location',
],
Step 4 : Create Controller
Now create a controller on this path app\Http\Controllers\UserController.php and add the below command.
<?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
We need to add a route for the details view file.
<?php
use Illuminate\Support\Facades\Route;
Route::get('ip_details', 'UserController@ip_details');
Step 6 : Create Blade File
Now, create a details.blade.php file to get current user location details in this path resources\views\details.blade.php and add the below HTML code.
<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 - websolutionstuff.com</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 the below output.
You might also like:
In this article, we will see how to add the default value of a column in laravel 10 migration. Here, we will learn...
May-01-2023
In this article, we will see how to get a client's IP address in laravel 10. Here we will learn about retrieving the...
Apr-07-2023
For finding the right web hosting provider, you need to do a lot of research. There are hundreds of web hosting provider...
Nov-12-2021
In this article, we will see how to create a table using migration in laravel 10. Migrations are like version contr...
Apr-21-2023