How To Get Current User Location In Laravel

Websolutionstuff | Jun-10-2020 | Categories : Laravel PHP

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

So, let's see how to get current user location in laravel, how to get current user location details in laravel, get the location in laravel, get location using the IP address in laravel, and how to get current user location using the IP address in laravel.

So, let's start and follow the below step one by one.

   Step 1 : Install Laravel

Type the following command in the terminal to create a new project in your system.

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

 

 

Step 2 : Install stevebauman/location Package In Your Application

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

composer require stevebauman/location

 

  Step 3 : Add Service Provider And Aliase

After package installation, we need to add service provider and aliase in config/app.php.

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

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

 

 

 Step 4 : Create Controller

Now create a controller on this path app\Http\Controllers\UserController.php and add the below command.

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

We need to add a route for the details view file.

<?php

use Illuminate\Support\Facades\Route;

Route::get('ip_details', 'UserController@ip_details');

 

 

Step 6 : Create Blade File 

Now, create a details.blade.php file to get current user location details in this path resources\views\details.blade.php and add the below HTML code.

<html>
<head>
	<title>How to get current user location in laravel</title>
</head>
<body style="text-align: center;">
	<h1> How to get current user location in laravel - websolutionstuff.com</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>

 

So, finally, we are done with our code we can get the below output.

current_user_locaation


You might also like:

Recommended Post
Featured Post
How To File Upload With Progress Bar In PHP
How To File Upload With Progre...

In this article, we will see how to file upload with a progress bar in PHP using ajax jquery. The file upload...

Read More

Sep-21-2022

Laravel 9 Barcode Generator Example
Laravel 9 Barcode Generator Ex...

In this article, we will see laravel 9 barcode generator example. In this example we will use the milon/barcod...

Read More

Mar-26-2022

Laravel 8 Mobile Number OTP Authentication using Firebase
Laravel 8 Mobile Number OTP Au...

Hello All, In this tutorial i will show you laravel 8 mobile number OTP authentication using firebase, There are many...

Read More

Mar-31-2021

Laravel 9 One To One Relationship Example
Laravel 9 One To One Relations...

  In this article, we will see laravel 9 one to one relationship example. Also, you can use one to one re...

Read More

Apr-01-2022