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 Use Sweetalert2 In Laravel
How To Use Sweetalert2 In Lara...

Today we will learn how to use sweetalert2 In laravel, You can use sweetalert2 in laravel as well as php, ...

Read More

May-03-2021

Laravel 8 Create Custom Helper Function Example
Laravel 8 Create Custom Helper...

In this article, we will see laravel 8 create a custom helper function example. As we all know laravel provides man...

Read More

Oct-12-2020

Datatables Expand/Collapse Columns
Datatables Expand/Collapse Col...

In this article, we will see how to expand/collapse columns in datatable. The Datatables API has a number of method...

Read More

Jun-05-2022

Routing - Laravel 7/8 Routing Example
Routing - Laravel 7/8 Routing...

In this article, we will give you information about the basic route, named route, and advanced route in laravel 7 and la...

Read More

Nov-01-2020