The event auth.login can be used to store it.
Can We Track last login date of authorised user
Can We Track last login date of authorised users In Laravel 5 . In user table i have two fields created_at and updated_at. How can we track.And show last login date
@Alizey When someone logs in capture the timestamp from the auth.login event and store it somewhere.
@richard i dont find any auth.login event in event folder
My bad, that was in Laravel 4. For Laravel 5, try this approach https://mattstauffer.co/blog/laravel-5.0-events-and-handlers
@Alizey events auth.login and auth.logout are already fired by default in L5.
Just add listener binding to your EventServiceProvider for example:
protected $listen = [
'auth.login' => [
'App\Handlers\Events\Somehandler@onUserLogin',
],
'auth.logout' => [
'App\Handlers\Events\SomeHandler@onUserLogout',
],
];
and
create your App\Handlers\Events\SomeHandler for example
public function onUserLogin($event)
{
// do something
}
public function onUserLogout($event)
{
//do something
}
@janareit Thanks :D i follow that
In Laravel 5.3, you can accomplish this by creating an event/listener binding as follows.
In your EventServiceProvider service provider (app/Providers/EventServiceProvider.php) add the following to the $listen array:
'Illuminate\Auth\Events\Login' => [
'App\Listeners\Users\UpdateLastLoggedInAt',
],
Then run php artisan event:generate. This will create your event listener located at app\Listeners\Users\UpdateLastLoggedInAt.
In the event listener, add the following to the handle() method (making sure you use Carbon\Carbon within your listener class):
public function handle(Login $event)
{
$event->user->last_logged_in_at = Carbon::now();
$event->user->save();
}
Note: Make sure you have a last_logged_in_at column in your users table and that you make it fillable.
Update: If you wanted the currently logged in user to also be able to view their own last login time as well as admins, then you could hook into the Illuminate\Auth\Events\Logout event and then update the last_logged_in_at field when the user logs out. That way, both admins and users should have a fairly accurate idea of this info.
Hello, I know this topic is several years old but it has served me a lot, I only have a problem with the save(); method It returns an error that the method is not defined, it still works fine, but I don't know why that error occurs ... Any ideas? Thanks!
Laravel 8
Will this actually display the current login instead of the previous timestamp where the user logged in?
It will @yansusanto yes, but I assumed that the OP wanted to have a way to track when the users of his app had last logged in so that he could see it himself, rather than user being able to see this info.
If he wants a way to track last logged in and current (so the currently logged in user can see their last logged in time), then he'd probably have to have two extra fields last_logged_in_at and current_login_time, then as a user logs in, take the old current_login_time value and update last_logged_in_at field with that time and the current_login_time with the current time. Unless there's another way to do this.
I suppose, you could create an event listener that listened for when the user logs out, Illuminate\Auth\Events\Logout and then update the last_logged_in_atfield.
Ah I see, that's for the Admin to see users' login activities.
I'm trying to figure out how to display user's last login for that user to track.
Well, probably use the Illuminate\Auth\Events\Logout event instead of the Login event @yansusanto and keep the rest the same. That way that field will only ever be updated when the user logs out.
If you wanted a completely accurate log in time, then store the current timestamp in the session as the user logs in, and then save it in the DB using the Logout event when the user logs out.
Thanks, @garethdaine. I'll give it a try.
By the way, by default I could use $user->created_at->diffForHumans().
Why I am not able to do the same with $user->last_logged_in_at->diffForHumans()?
Ah I forgot to update my User Model
protected $dates = ['last_logged_in_at'];
or can we store in session the last value of last_logged_in_at and echo out in view after updating the record?
@garethdaine : Works perfect for me. Thanks for the explanation!
@garethdaine thanks...
Your solution works perfectly for me.
don't do anything that tracks logout as most users just close their browser or go away, hence logout never runs
I am actually storing two timestamps on the user for this purpose. The column names are last_login, and previous_last_login. When the login event fires, the previous_last_login field is updated with the last_login timestamp, then last_login is updated with the current date/time.
In my use case, I wanted to display a list of items that had been added since the user last logged in. I can do that by referencing previous_last_login field against the timestamp on the list of items.
Another way to accomplish this is by using session, or cookies. But I think this gives you better flexibility. You could also define the login events on a separate table and update them through an eloquent relationship to the user.
Hi all, Is the post from @garethdaine still working on laravel 5.6? It wasn't working when I tried. Timestamp doesn't update
If you came in search of a solution. Just add the below line in your 'AppServiceProvider' 'boot' method:
Event::listen(function (Login $event) {
/** @var \App\Models\User */
$user = $event->user;
Model::withoutTimestamps(fn () => $user->touchQuietly('last_logged_in_at'));
});
Where:
use Illuminate\Auth\Events\Login;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;
Don't forget the table schema and model:
$table->timestamp('last_logged_in_at')->nullable();
public function casts(): array
{
return [
'last_logged_in_at' => 'datetime',
];
}
Please or to participate in this conversation.