Chris
Chris

Reputation: 4436

Update the user when authenticated in Laravel

I have a "last_login" column in my users table that I'd like to update when a user logs in. I added this to the controller of my login post page:

DB::select('UPDATE '.DB::getTablePrefix().'users SET last_login=now() WHERE id=?', array($user->id));

But I have more than one login option (through social networks and such) for users. I could add this code to each of those options, but I'm wondering if there's a way to do it when Auth runs its authentication code to prevent having to add it to each controller method where I call it.

How could I do that, maybe by extending Auth or something?

Upvotes: 0

Views: 52

Answers (1)

user1669496
user1669496

Reputation: 33068

This is an example right on the docs here... http://laravel.com/docs/events#basic-usage

Event::listen('auth.login', function($user)
{
    $user->last_login = new DateTime;

    $user->save();
});

Also be sure to check the "Where to register events" section as it can give some good ideas on where to put this piece of code.

Upvotes: 2

Related Questions