In this tutorial, you'll learn how to record the date your users last logged in to Laravel.
About Laravel Login
In Laravel, login, and registration, are functionalities that can be easily deployed with the use of a command.
We're talking specifically about the command php artisan make:auth.
This command will generate some blade files and drivers for us, necessary for the login and user registration to work in our project.
So, a First Alternative would be thinking about modifying the file LoginController.
If we modify one of its methods, specifically one that is responsible for creating the user's session after validating their credentials, we could capture the Login Date.
Events in Laravel
However, there is a better way to achieve this.
And it is, using Laravel events.
This way we can avoid the complexity of modifying LoginController and continue to keep our code tidy.
¿How we do it then?
Listen for the sign-in event
That's right, the first thing we must do is detect the moment when a user has just logged in successfully.
At that moment we will save the date.
So let's app\Providers\EventServiceProvider.php.
If this is the first time you enter here you will find the following:
protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
],
];
This array contains an event SomeEvent example, and a listener EventListener, also as an example.
What it means is that when it happens SomeEvent, The EventListener The associate will listen to that event and will take care of acting in a certain way.
¿That means we can create our own events and our own listeners?
That's right.
We can define our own events, and activate them when we deem it necessary.
But this is not the case.
The login event does not have to be activated by us. It is an event that Laravel already makes available to us.
To listen to and act on the login event, we'll use the following:
protected $listen = [
'Illuminate\Auth\Events\Login' => [
'App\Listeners\SuccessfulLogin',
],
];
As you can see, the Login event is already predefined. But our Listener is in a folder at our fingertips.
How we created our listener?
After having modified the EventServiceProvider, Next is to create the listener SuccessfulLogin.
Laravel makes this task easier for us. Well, we simply have to execute:
php artisan event:generate
If you did everything right, the console will surely respond to you: Events and listeners generated successfully!
And inside the folder app\Listeners You will find the class SuccessfulLogin.
Open it, and inside you will find a method handle.
Last step: save the date
At this point, you can do with the date what you see fit:
-
If you're only interested in knowing the last date a user logged in, simply add one more column to your user table.
-
If you want to be able to display a history with all the dates you logged in instead, then you should create an additional table for that.
For this example, let's consider the first option.
So in the migration of users we will have the following:
$table->dateTime('last_login')->nullable();
And in the method handle:
public function handle(Login $event)
{
$event->user->last_login = new DateTime;
$event->user->save();
}
Frequently Asked Questions
You're probably wondering what the object represents $event.
This object comes as a parameter in the handle, and this is because when Laravel triggers the Login event, it sends that information.
It can work in the same way, if you define your own events.
When you trigger events, you can send parameters in this way, so that the listener acts properly.
To help you understand it better, you can watch the following video:
Incorrect time
If your app is recording the date and time with a value that doesn't match your country's time.
Chances are, you haven't defined the Time zone of your Laravel project.
To do this you must go to config\app.php.
There you will find:
'timezone' => 'UTC',
In my case, I'm in Peru, so I'll change the time zone like this:
'timezone' => 'America/Lima',
And the result:
Conclusion
Laravel defines many events that will be very useful to us in our project.
And if we want to define our own events, we can also do that.
For example, if we want to send a confirmation email when a user makes a post in our app, we can use an event that listens for this successful log.
If you have any questions, or think we can improve this guide, please leave a comment.
Thanks for reading to the end.



