How To Get Current User Location In Laravel 9

Websolutionstuff | Mar-23-2022 | Categories : Laravel PHP

In this article, we will see how to get the current user location in laravel 9. Many times we are required to find the current location of users for many purpose. So, here I am using stevebauman/location laravel package, Using this package you can get many information of utilizer like postal code, zip code, region denomination, state name longitude, country denomination, latitude, iso code, etc.

So, let's get the location in laravel 9, get location using an IP address in laravel 9, how to get current user location using an IP address in laravel 9, stevebauman/location in laravel 9.

   Step 1 : Install Laravel 9

In this step, we are creating the laravel 9 project using the below command.

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

 

 

Step 2 : Install stevebauman/location Package In Your Application

After installation of project you need to install stevebauman/location Package

composer require stevebauman/location

 

  Step 3 : Add Service Provider And Aliase

Add the service provider in config/app.php file.

If you're using Laravel 5.5 or above, you can skip the registration of the service provider, as it is registered automatically.

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

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

 

 

 Step 4 : Create Controller

Now create controller and add the below code in the app\Http\Controllers\UserController.php file.

<?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

Now, we need to add route in the routes\web.php file

<?php

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


Route::get('ip_details', [UserController::class,'ip_details']);

 

 

Step 6 : Create Blade File

Now, create details.blade.php file for get current user location details in this path resources\views\details.blade.php and add below html code.

<html>
<head>
	<title> How To Get Current User Location In Laravel 9 - Websolutionstuff </title>
</head>
<body style="text-align: center;">
	<h1> How To Get Current User Location In Laravel 9 - Websolutionstuff </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>

Output :

how_to_get_current_user_location_in_laravel_9_output

 


You might also like :

Recommended Post
Featured Post
Laravel 9 Vue JS CRUD Operation Example
Laravel 9 Vue JS CRUD Operatio...

In this article, we will see the laravel 9 vue js crud operation example. Here, we will learn how to create a vue 3...

Read More

Dec-07-2022

Laravel 10 AJAX Form Validation Example
Laravel 10 AJAX Form Validatio...

In this article, we will see how to validate the ajax form in laravel 10. Here we will learn about the laravel 10 a...

Read More

Mar-24-2023

How To Generate RSS Feed In Laravel 9
How To Generate RSS Feed In La...

In this article, we will see how to generate an RSS feed in laravel 9. Here, we will generate an RSS feed in larave...

Read More

Dec-12-2022

Laravel 9 whereNull / whereNotNull Query Example
Laravel 9 whereNull / whereNot...

In this article, we will see the laravel 9 whereNull / whereNotNull query example. In laravel 9 additional where clauses...

Read More

Oct-18-2022