How to get client IP address in Laravel 5+

In Laravel, you can get the client's IP address by using the Request facade's ip method. Here's an example of how you can use it:

use Illuminate\Http\Request;

Route::get('/', function (Request $request) {
    $ip = $request->ip();
    // Do something with the IP address...
});

Watch a course Learn object oriented PHP

You can also get the IP address from the global $request variable if you don't want to type-hint the Request facade:

Route::get('/', function () {
    $ip = request()->ip();
    // Do something with the IP address...
});

Keep in mind that the ip method will return the client's "real" IP address if the request is passed through a proxy. If you want to get the IP address of the proxy, you can use the server method like this:

$proxyIp = $request->server('HTTP_X_FORWARDED_FOR');