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
Drag and Drop File Upload Using Dropzone js in Laravel 9
Drag and Drop File Upload Usin...

In this article, we will see drag and drop file upload using dropzone js in laravel 9. Dropzone JS is an open-source lib...

Read More

Mar-19-2022

How to Create New Component in Angular 17 using Command
How to Create New Component in...

Welcome to this comprehensive tutorial where I'll guide you through the process of creating a new component in Angul...

Read More

Mar-22-2024

Angular 13 Crop Image Before Upload With Preview
Angular 13 Crop Image Before U...

In this article, we will see the angular 13 crop image before uploading with a preview. we will give you a sim...

Read More

May-10-2022

How To Create Pagination Using jQuery
How To Create Pagination Using...

In this article, we will see how to create pagination using jquery. We will create jquery pagination using mul...

Read More

Nov-08-2022