How To Get Current User Location In Laravel

May 24, 2021 . Admin

Hello Friends,

Now let's see example of how to get current user location in laravel. This is a short guide on laravel if get current user location. We will use how to use current user location in laravel. Here you will learn how to use current user location in laravel. We will use how to get if current user location in laravel. Let's get started with how to current user location get in laravel.

Here, I will give you many example how you can get current user location in laravel.

Step 1 : Install Laravel

Type the following command in terminal for create 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 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 controller on this path app\Http\Controllers\CompanyController.php and add below command.

Path: app\Http\Controllers\CompanyController.php

<?php

	namespace App\Http\Controllers;

	use Illuminate\Http\Request;
	use App\Company;

	class CompanyController extends Controller
	{
		/**
		* Write code on Method
		*
		* @return response()
		*/
	    public function locationinfo()
	    {
	        $ip = '123.136.207.104'; //For static IP address get
	        //$ip = request()->ip(); //Dynamic IP address get
	        $data = \Location::get($ip);
	                      
	        return view('info',compact('data'));
	    }
	}
?>
Step 5 : Add Route

We need to add route for details view file.

Path: routes\web.php

<?php

	use Illuminate\Support\Facades\Route;

	use App\Http\Controllers\CompanyController;

	/*
	|--------------------------------------------------------------------------
	| Web Routes
	|--------------------------------------------------------------------------
	|
	| Here is where you can register web routes for your application. These
	| routes are loaded by the RouteServiceProvider within a group which
	| contains the "web" middleware group. Now create something great!
	|
	*/

	Route::get('locationinfo', [CompanyController::class, 'locationinfo']);
?>
Step 6 : Create Blade File

Now, create details.blade.php file for get current user location details in this path

resources\views\info.blade.php

and add below html code.

Path: resources\views\info.blade.php

<html>
	<head>
		<title>How to get current user location in laravel -  MyWebtuts.com</title>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
	</head>
	<body class="bg-dark mt-5">
		<div class="container">
			<div class="row">
						<div class="col-md-8 offset-md-4">
				<div class="card w-50">
					<div class="card-header text-center">
						<h5>How to get current user location in laravel -  MyWebtuts.com</h5>
					</div>
					<div class="card-body text-center">
							<h5>IP: {{ $data->ip }}</h5>
							<h5>Country Name: {{ $data->countryName }}</h5>
							<h5>Country Code: {{ $data->countryCode }}</h5>
							<h5>Region Code: {{ $data->regionCode }}</h5>
							<h5>Region Name: {{ $data->regionName }}</h5>
							<h5>City Name: {{ $data->cityName }}</h5>
							<h5>Zipcode: {{ $data->zipCode }}</h5>
							<h5>Latitude: {{ $data->latitude }}</h5>
							<h5>Longitude: {{ $data->longitude }}</h5>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
</html> 

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

It will help you....

#Laravel 8 #Laravel