Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - last login date and time timestamp

I know there are fields automatically inserted (e.g. updated_at and created_at) but I was wondering if there is like an Eloquent method timestamp() or the Laravel way to produce timestamps?

For instance, I want to record a timestamp every time a user logs in to my system and store it into the database, so we can see each user's last login date and time details.

like image 437
jonprasetyo Avatar asked Mar 17 '14 16:03

jonprasetyo


4 Answers

In Laravel 5.2+ (tested on 5.6 as well) the process is a little different. First, add your listener to the EventServiceProvider:

protected $listen = [
    'Illuminate\Auth\Events\Login' => [
        'App\Listeners\LogSuccessfulLogin',
    ],
];

Then do:

php artisan event:generate

Which will create a new listener in app/Listeners. Now edit your Listener to touch the date in the last_login column in your users table:

public function handle(Login $event)
{
    $event->user->last_login = date('Y-m-d H:i:s');
    $event->user->save();
}

Make sure the Listener created by Artisan has the correct namespaces at the top. For this particular case the correct namespaces in your Listener should be:

use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
like image 63
Dan H Avatar answered Oct 07 '22 15:10

Dan H


Just add this code below into your App\Http\Controllers\Auth\LoginController.php

At the top of your controller:

use Carbon\Carbon;
use Illuminate\Http\Request;

And inside your controller put this function below:

public function authenticated(Request $request, $user) {
    $user->last_login = Carbon::now()->toDateTimeString();
    $user->save();
}
like image 43
Rafael Xavier Avatar answered Oct 07 '22 16:10

Rafael Xavier


You may observe the auth.login event and update the user's last login time. See the first example in the documentation for events to accomplish exactly what you're trying to do.

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

    $user->save();
});

Note: In 4.0, the event name is user.login. In 4.1, the event name is auth.login

Update

It's really up to you where you put your Event listeners. If you read the linked to doc page, it states:

So, you know how to register events, but you may be wondering where to register them. Don't worry, this is a common question. Unfortunately, it's a hard question to answer because you can register an event almost anywhere! But, here are some tips. Again, like most other bootstrapping code, you may register events in one of your start files such as app/start/global.php.

If your start files are getting too crowded, you could create a separate app/events.php file that is included from a start file. This is a simple solution that keeps your event registration cleanly separated from the rest of your bootstrapping. If you prefer a class based approach, you may register your events in a service provider. Since none of these approaches is inherently "correct", choose an approach you feel comfortable with based on the size of your application.

My personal preference would be to register them within a Service Provider, since that would keep them segregated and to me is the more 'Laravel' way of doing things. If you need help with service providers, see this documentation entry.

However, if you really just want to get something up and running as quickly as possible, it would be just as easy for you to register that listener inside of app/start/global.php

like image 45
Jeff Lambert Avatar answered Oct 07 '22 15:10

Jeff Lambert


I had a similar question. In case you don't feel the need the use events you can update the timestamp after you authenticate the user. I put the following code in my controller method that handles the user authentication.

if (Auth::attempt(array('email'=>$email, 'password'=>$password))) {
    Auth::user()->last_login = new DateTime();
    Auth::user()->save();
    return Redirect::intended('/');
} else {
    return Redirect::to('account/login');
}

This does the job for me.

like image 26
jor Avatar answered Oct 07 '22 16:10

jor