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 Change Datepicker Start Date In Angular 15
How To Change Datepicker Start...

In this tutorial, I will guide you through the process of changing the start date of a datepicker component in Angular 1...

Read More

Jul-03-2023

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

How To Validate Multi Step Form Using jQuery
How To Validate Multi Step For...

In this article, we will see how to validate multi step form wizard using jquery. Here, we will learn to validate t...

Read More

Jan-27-2023

Laravel 9 Flash Message Example
Laravel 9 Flash Message Exampl...

In this article, we will see a laravel 9 flash message example. Here, we will learn how to create or implement flash mes...

Read More

Dec-27-2022