How To Get Client IP Address In Laravel ?

Laravel 8 , Laravel 7 , Laravel , Laravel 6

Nicesnippets

10103

21-05-2021


How To Get Client IP Address In Laravel ?

Hi guys, Today i will explained 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 just follow my step by step and learn How to get Client IP address in Laravel.

In this tutorial, you will learn How to get Client IP address in Laravel.

Here i will give you example for laravel get client Ip Address. So let's see the bellow example.

Solution


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

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

Step 1: Create Route

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

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\HomeController;

/*

|--------------------------------------------------------------------------

| 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('client/ip', [HomeController::class, 'getIp']);

Step 2: Create a HomeController

Next you can require to the HomeController so create a HomeController in just following command through.

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 we can get below output.

php artisan serve

http://localhost:8000/client/ip

It will help you...


Recommended Posts