How to Get Current User Location in Laravel 9?


Share:
 | Published By: Admin | Published On: May 02, 2022 | Category: Laravel 9

Hello Friends,

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

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

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Install stevebauman/location Package

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.

php artisan make:controller CompanyController
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: Create Route

We need to add route for details view file.

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.

resources\views\info.blade.php
<html>
<head>
    <title>How to get current user location in laravel 9 -  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 9 -  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> 
Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/locationinfo
Output:

It will help you....

Share: