Site Admin
Site Admin Founder of MeaningArticles
1315 Views

Laravel 8 How to get IP Address?

Hello Dev.

today i will defined to the how to get client IP address in your laraval project. how to get client IP address in Laravel is so easy to use. so you can simply comply with my little by little and discover ways to get client IP address in Laravel.

on this article, you may discover ways to get client IP address in Laravel.

right here i'm able to give you example for laravel get client IP address. So let's have a look at the bellow instance.


The Trick

$method1 = request()->ip();
$method2 = request()->getClientIp();


Step 1: Create Route

create a route in web.php file and use this code.
web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| here is wherein you may register web routes for your application. these
| routes are loaded with the aid of the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something tremendous...!
|
*/
Route::get('client/ip', [HomeController::class, 'getIp']);


Step 2: Create Controller

next you may require to the HomeController so create a HomeController and put just following command.
app/Http/Controllers/HomeController.php

<?php

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

class HomeController extends Controller
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function getIp(Request $request)
    {
        $method1 = request()->ip();
        $method2 = request()->getClientIp();
        dd($method1);
    }
}

So, finally we are done with our code run server and open url in browser.

php artisan serve

open in web browser...

http://localhost:8000/client/ip

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.