In this tutorial, we will see how you can get the current user location, city, country, country code, postal code, zip code, region, longitude and latitude using IP Address in laravel.
So we will be using stevebauman/location
package in order to get that information.
So let’s get started.
Table of Contents
Step 1: Install Laravel
First, we will install laravel into the project. I am installing it in user_location
folder.
composer create-project --prefer-dist laravel/laravel user_location
Step 2: Install stevebauman/location package
Then we need to install the stevebauman/location
package.
composer require stevebauman/location
Step 3: Add the Service Provider and Alias
Then we need to add the service provider in config/app.php
in providers
array:
Stevebauman\Location\LocationServiceProvider::class,
And also add following alias in aliases
in config/app.php
:
'Location' => Stevebauman\Location\Facades\Location,
So your final providers
array will look like this:
And aliases
array will look like this:
Step 3: Create controller and add Route
Now we will create the userController
:
php artisan make:Controller UserController
and will add route:
Route::get('/user',[\App\Http\Controllers\UserController::class,'user']);
Step 4: Create the user function in User controller
Now we will create the user
function in the userController
.
public function user()
{
$ip = '206.84.142.1'; //static ip
//$ip = request()->ip(); //ip of the user which is currently visiting.
$data = \Location::get($ip);
return view('home',compact('data'));
}
In this user
function, I am using the static IP $ip = '206.84.142.1';
since I am developing it on localhost, if you are using it on a production server, simply use $ip = request()->ip();
. Then we are passing the data to the resources/views/home.blade.php
view.
Step 5: Create the home view
Create a new view resources/views/home.blade.php
<html>
<head>
<title>Get User Details in laravel</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<style>
.card{
width: 18rem;
margin: auto;
}
</style>
</head>
<body class="text-center">
<h4 class="mt-2"> How to get current user details in laravel</h4>
<a href="https://impulsivecode.com">impulsivecode.com</a>
<div>
<br>
<div class="card text-center">
<ul class="list-group list-group-flush">
<li class="list-group-item"><strong>IP:</strong> {{ $data->ip }}</li>
<li class="list-group-item"><strong>Country:</strong> {{ $data->countryName }} </li>
<li class="list-group-item"><strong>Country Code:</strong> {{ $data->countryCode }} </li>
<li class="list-group-item"><strong>Region:</strong> {{ $data->regionName }} </li>
<li class="list-group-item"><strong>Region Code:</strong> {{ $data->regionCode }} </li>
<li class="list-group-item"><strong>City:</strong> {{ $data->cityName }} </li>
<li class="list-group-item"><strong>Zip Code:</strong> {{ $data->zipCode }} </li>
<li class="list-group-item"><strong>Latitude:</strong> {{ $data->latitude }} </li>
<li class="list-group-item"><strong>Longitude:</strong> {{ $data->longitude }} </li>
</ul>
</div>
</div>
</body>
</html>
In this view, I am using bootstrap to style the information. Basically, we are getting all the details in the data
variable and showing it.
Step 6: Run the application
Run the application:
php artisan serve
And visit the page /user
to see the details.
Final Output will be like:
Also Check the article on getting user agent in laravel
Conclusion:
Hope you have learned how to get the current user location, city, a country in laravel. Let me know if you face any problems.
See you soon.
Write a Reply or Comment