Capture Last Login Time in Laravel 5

In this tutorial, we are going to capture the login time and date of the user and save it to database. To do this we are going to use Laravel handlers.

Useful Read : Set friendly names for validation attributes in Laravel 5

To save the date and time to the database, first we need to add a last_login column to the users table. To add the last_login column, create migration by using the following command.

php artisan make:migration add_last_login_column_to_users_table --table=users

This will generate a new migration file in the database\migrations folder.

Add the following line to your migration file.


Now, type the following command to execute migration file.

php artisan migrate

This will create a new column within your users table.

Now generate a new event handler by using following command.

php artisan handler:event LoginEventhandler

This will create a new event handler within App/Handlers/Events.

Laravel Event Handler

Laravel Event Handler

As you can see, within the handle method we are using Carbon package to capture current date and time information.

Now, all we need to do is hook the event handler with login event.

To associate this handler with the login event navigate to App\Providers and open EventServiceProvider.php file and add the following code to $listen array.

'auth.login' => [

And that’s it. Now, when a user will login to his account the event handler will update last_login timestamp with correct value.


Leave a Comment.