Recording last login information using Laravel events
Posted on
Laravel’s events provide a simple observer implementation, allowing you to listen for various events that occur in your application. Laravel raises several events throughout the authentication process. This example shows how to attach a listener for the Illuminate\Auth\Events\Login event and store when a user has last logged in and from what IP address.
Registering events
Edit the EventServiceProvider included in your Laravel application to register the event listener.
Use the artisan event:generate command to generate the LogSuccessfulLogin listener.
Edit the LogSuccessfulLogin listener to add the event handling logic.
Database migrations
The above implementation records the last login information in two columns on the users table. Use the artisan make:migration command to create the migrations for each of the columns that will be added to the users table.
Edit the migrations to add the last_login_at and the last_login_ip columns.
Use the artisan migrate command to run the migrations.