In this article, I will show you How To Get Client Ip Address in laravel 12 application.
There are several ways to get an IP address in a Laravel 12 application. We will use the `request()` helper, `$request` object, and Request facade to get the client’s IP address in Laravel 12.
How To Get Client Ip Address in laravel 12 application.
Example 1: Laravel 12 Get IP Address using $request Object
you can use the ip() method on the Request object. This method will return the IP address of the client, or it will return null if the IP address cannot be determined.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$clientIP = $request->ip();
dd($clientIP);
}
}
Example 2: Laravel 12 Get IP Address using request() Helper
you can use the ip() method with request() helper function. you can use this helper method in laravel blade file as well.
<p>{{ request()->ip() }}</p>
Example 3: Laravel 12 Get IP Address using Request Facade
you can use the ip() method on the Request facade. This method will return the IP address of the client, or it will return null if the IP address cannot be determined.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$clientIP = Request::ip();
dd($clientIP);
}
}
Example 4: Laravel 12 Get IP Address using getClientIp()
you can use the getClientIp() method on the request object. This method will return the IP address of the client, or it will return null if the IP address cannot be determined.
Read Also Laravel 12 Send Email using Queue Example
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$clientIP = $request->getClientIp();
dd($clientIP);
}
}