ItSolutionStuff.com

Laravel 11 Get Client IP Address Example

By Hardik Savani • September 4, 2024
Laravel

In this article, I will show you how to get client ip address in laravel 11 application.

There are several ways to get an IP address in a Laravel 11 application. We will use the `request()` helper, `$request` object, and Request facade to get the client's IP address in Laravel 11.

So, let's see the examples one by one:

laravel 11 get ip address

Example 1: Laravel 11 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 11 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 11 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 11 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.

<?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);
    }
}

i hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel 11 Ajax CRUD Operation Tutorial Example

Read Now →

Laravel 11 Cron Job Task Scheduling Tutorial

Read Now →

Laravel 11 Send Email using Queue Example

Read Now →

Laravel 11 Change Date Format Examples

Read Now →

Laravel 11 Yajra Datatables Example Tutorial

Read Now →

Laravel 11 REST API Authentication using Sanctum Tutorial

Read Now →

Laravel 11 Ajax Form Validation Example Tutorial

Read Now →

Laravel 11 Authentication - Install Laravel 11 Breeze Tutorial

Read Now →

How to Send Email using Gmail in Laravel 11?

Read Now →

Laravel 11 Vue JS Auth Scaffolding with Vite Tutorial

Read Now →

Laravel 11 Import Export Excel and CSV File Tutorial

Read Now →

Laravel 11 Generate PDF File using DomPDF Example

Read Now →

Laravel 11 CRUD Application Example Tutorial

Read Now →

How to Create and Use Traits in Laravel 11?

Read Now →