Laravel - Get Location From IP Address

version : Laravel 10
Post Time : 05-07-2024

Laravel - Get Location From IP Address

install laravel


        -   composer create-project --prefer-dist laravel/laravel:^10.0 laravel_demo


To get location from IP Address


         -  composer require stevebauman/location


Create DemoController Controller  :


         -  php artisan make:controller DemoController


 app/Http/Controllers/DemoController.php

                                      
                                        

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Location;

class DemoController extends Controller
{
   public function getUserLocation(Request $request)
   {
       $ip = '103.85.106.19';
       $position = Location::get($ip);
       dd($position);
   } 
}

routes/web.php

                                      
                                        

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DemoController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
   return view('welcome');
});

Route::get('/get-user-location', [DemoController::class, 'getUserLocation'])->name('getUserLocation');