Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Alizey's avatar

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

0 likes
21 replies
dishark's avatar

The event auth.login can be used to store it.

richard's avatar

@Alizey When someone logs in capture the timestamp from the auth.login event and store it somewhere.

janareit's avatar

@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
    }
4 likes
garethdaine's avatar

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.

10 likes
RodrigoG's avatar

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

yansusanto's avatar

@garethdaine

Will this actually display the current login instead of the previous timestamp where the user logged in?

garethdaine's avatar

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.

1 like
yansusanto's avatar

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.

garethdaine's avatar

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.

1 like
yansusanto's avatar

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()?

1 like
yansusanto's avatar

Ah I forgot to update my User Model

protected $dates = ['last_logged_in_at'];
juliouslara's avatar

or can we store in session the last value of last_logged_in_at and echo out in view after updating the record?

Snapey's avatar

don't do anything that tracks logout as most users just close their browser or go away, hence logout never runs

1 like
vbrimley's avatar

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.

timmy1420's avatar

Hi all, Is the post from @garethdaine still working on laravel 5.6? It wasn't working when I tried. Timestamp doesn't update

santosvilanculos's avatar

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.