Laravel - last login date and time timestamp

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.




4 Answers
4



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.


auth.login


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


user.login


auth.login



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


app/start/global.php





Wow thanks for the reply. It seems like exactly what I wanted. However how do I use this? I am new to Laravel
– jonprasetyo
Mar 17 '14 at 16:56





@jonprasetyo Updated my answer
– Jeff Lambert
Mar 17 '14 at 18:22





thanks again! I just tried putting the block of code into the global.php and it works! Will definitely check out the Service Provider.
– jonprasetyo
Mar 18 '14 at 16:07





Latest reference for the Laravel Auth Events: laravel.com/docs/authentication#events
– Dan H
Dec 30 '15 at 1:07




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 = [
'IlluminateAuthEventsLogin' => [
'AppListenersLogSuccessfulLogin',
],
];



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 IlluminateAuthEventsLogin;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;





Awesome, I would have never figured this out on my own. If you would rather use the time in the DB, you could do:<br> use IlluminateSupportFacadesDB; <br>... <br> $event->user->last_login = DB::raw('now()');
– Ben
May 20 '16 at 9:37



use IlluminateSupportFacadesDB;


$event->user->last_login = DB::raw('now()');





How does the $event object get the user object into it? Is this event fired deep in Laravel somewhere?
– Squiggs
Jan 10 '17 at 20:27





The Login event already exists. It lives here: IlluminateAuthEventsLogin and it's passed to your method whenever you add a listener to IlluminateAuthEventsLogin. It all happens automatically.
– Dan H
Jan 11 '17 at 14:23





FYI InteractsWithQueue is only needed if you need interaction (delete, release, etc) with the queue, in this case it's not necessary.
– Matt K
Mar 30 '17 at 18:55


InteractsWithQueue


delete


release



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.





I think that in this solution you miss logins by autologin mechanism.
– Adiasz
Mar 18 '15 at 7:32



Just add this code below into your AppHttpControllersAuthLoginController.php



At the top of your controller:


use CarbonCarbon;
use IlluminateHttpRequest;



And inside your controller put this function below:


public function authenticated(Request $request, $user)
$user->last_login = Carbon::now()->toDateTimeString();
$user->save();





Perfect solution. Simple.
– timothymarois
Sep 10 at 17:20






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)