How To Get Current User Location In Laravel 9

in this blog post i will give get current user location in laravel 9,i am using stevebauman/location packages of how get current user location in laravel 9.in this package included all of the lattitude, longitude, ip , region name, zip code, postal code, city name, country name, county code

Step 1 : how to get current user location in laravel 9

in step 1 coomand to follow by ..

composer create-project --prefer-dist laravel/laravel get_user_location

Step 2 : Install stevebauman/location Package In Your Project

after project installing to next command to install location package.. stevebauman/location

composer require stevebauman/location

 Step 3 : Add Service Provider And Aliase In Config.php

config.php

'providers' => [
	
	Stevebauman\Location\LocationServiceProvider::class,
],

'aliases' => [
	
	'Location' => 'Stevebauman\Location\Facades\Location',
],

Read More: How To Push Array Element In Node Js

 Step 4 : Create Controller :how to get current user location in laravel 9

Now create new controller  app\Http\Controllers\UserLocationController.php file.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;

class UserLocationController extends Controller
{
    public function ip_info()
    {
        $ip = '43.248.34.31'; //For My Static IP address
        //$ip = request()->ip(); //Dynamic IP address
        $data = \Location::get($ip);                
        return view('details',compact('data'));
    }
}

Step 5 : Add Route

Now, add this route in web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;


Route::get('ip_information', [UserLocationController::class,'ip_info']);

Step 6 : Create Blade File

In this last step 6 in create new blade file ipdetails.blade.php get this path to i hope this file create done. resources\views\ipdetails.blade.php

<html>
<head>
	<title> How To Get Current User Location In Laravel 9 - Laratuto.com</title>
</head>
<body style="text-align: center;">
	<h1> How To Get Current User Location In Laravel 9 - Laratuto.com</h1>
	<h3>Ip Address: {{ $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>

Output :

How To Get Current User Location In Laravel 9

Leave a Comment