Intro

Save a user’s last login timestamp and IP address in the users table.

Step 1 - Migration

Add too or create a new migration, similar to the following:

Schema::table('users', function (Blueprint $table) {
    $table->datetime('last_login_at')->nullable();
    $table->string('last_login_ip')->nullable();
});

Step 2 - Model

Next, we need to add these fields as fillables in app/User.php model:

protected $fillable = [
    'email',
    'password',
    'name',
    'last_login_at',
    'last_login_ip',
];

Step 3 - Method

Open app/Http/Controllers/Auth/LoginController.php, and add the following method:

function authenticated(Request $request, $user)
{
    $user->update([
        'last_login_at' => Carbon::now()->toDateTimeString(),
        'last_login_ip' => $request->getClientIp()
    ]);
}

Add use statement

use Illuminate\Http\Request;

Conclusion

All done, now, your application will logged users' most recent IP & timestamps to database.