Laravel 9 Get Current User Location From IP Address
Hi Dev,
In this quick example, let's see laravel 9 get user current location. I explained simply about how to get current user location in laravel 9. Here you will learn laravel 9 get location from ip. This post will give you simple example of laravel 9 get user location.
In this tutorial, we will use stevebauman/location composer package to get the current user location in laravel app. you can just follow below step and get the layout like below.
Preview:
Step 1: Install Laravel
first of all we need to get fresh Laravel 8 version application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Install stevebauman/location Package
here, we will install stevebauman/location package for getting current location on login user.
composer require stevebauman/location
Step 3: Create Route
In this is step we need to create some routes for add to cart function.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
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('display-user', [UserController::class, 'index']);
Step 4: Create Controller
in this step, we need to create UserController and add following code on that file:
app/Http/Controllers/UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Stevebauman\Location\Facades\Location;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
/* $ip = $request->ip(); Dynamic IP address */
$ip = '162.159.24.227'; /* Static IP address */
$currentUserInfo = Location::get($ip);
return view('user', compact('currentUserInfo'));
}
}
Step 5: Create Blade Files
here, we need to create blade file for user. so let's create one by one files:
resources/views/user.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>How to Get Current User Location with Laravel - ItSolutionStuff.com</h1>
<div class="card">
<div class="card-body">
@if($currentUserInfo)
<h4>IP: {{ $currentUserInfo->ip }}</h4>
<h4>Country Name: {{ $currentUserInfo->countryName }}</h4>
<h4>Country Code: {{ $currentUserInfo->countryCode }}</h4>
<h4>Region Code: {{ $currentUserInfo->regionCode }}</h4>
<h4>Region Name: {{ $currentUserInfo->regionName }}</h4>
<h4>City Name: {{ $currentUserInfo->cityName }}</h4>
<h4>Zip Code: {{ $currentUserInfo->zipCode }}</h4>
<h4>Latitude: {{ $currentUserInfo->latitude }}</h4>
<h4>Longitude: {{ $currentUserInfo->longitude }}</h4>
@endif
</div>
</div>
</div>
</body>
</html>
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/display-user
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 9 Socialite Login with Google Account Example
- Laravel 9 Multi Auth: Create Multiple Authentication in Laravel
- Laravel 9 REST API Authentication using Sanctum Tutorial
- Laravel 9 Markdown | Laravel 9 Send Email using Markdown Mailables
- Laravel 9 Mail | Laravel 9 Send Email Tutorial
- Laravel 9 React JS Auth Scaffolding Tutorial
- Laravel 9 Import Export Excel and CSV File Tutorial
- Laravel 9 PDF | Laravel 9 Generate PDF File using DomPDF
- Laravel 9 Create Custom Helper Functions Example
- Laravel 9 Form Validation Tutorial Example
- Laravel 9 CRUD Application Tutorial Example