Posted by Arjun on Saturday 3rd June 2017

How to record last login time and IP address of a user In Laravel

client IP How to Laravel 5 Laravel 5.1 Laravel 5.x Laravel 5.2 laravel 5.3 Laravel 5.4 last login time IP address

Recording last login information is useful and you can easily save and update the last login time and IP address of the client.

You can achieve this in several ways but I am gonna show you very simple and effective technique, just add the below-shown method to the login Controller - App\Http\Controllers\Auth\LoginController.php

 function authenticated(Request $request, $user)
    {
        $user->last_login = Carbon::now();
        $user->last_login_ip = $request->getClientIp();
        $user->save();
    }

If you see you can find the authenticated() method inside the \Illuminate\Foundation\Auth\AuthenticatesUsers.php trait file and this method will be called after each successful login. So we are overriding this method in our login controller to update the last login time and IP of the client.