Laravel 9 Get Current User Location From IP Address
By Hardik Savani July 1, 2022 Category : LaravelPauseUnmuteLoaded: 3.33%Seek to live, currently behind liveLIVERemaining Time -49:40FullscreenHi 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:
Step 2: Install stevebauman/location Package
here, we will install stevebauman/location package for getting current location on login user.
composer create-project --prefer-dist laravel/laravel blog
composer require stevebauman/location
<?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']);
<?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')); }}