php Laravel - 上次登录日期和时间时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22460066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - last login date and time timestamp
提问by jonprasetyo
I know there are fields automatically inserted (e.g. updated_atand created_at) but I was wondering if there is like an Eloquent method timestamp()or the Laravel way to produce timestamps?
我知道有自动插入的字段(例如updated_at和created_at),但我想知道是否有像Eloquent方法timestamp()或 Laravel 方法来生成时间戳?
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.
例如,我想在用户每次登录我的系统时记录一个时间戳并将其存储到数据库中,这样我们就可以看到每个用户上次登录的日期和时间详细信息。
采纳答案by Jeff Lambert
You may observe the auth.loginevent and update the user's last login time. See the first example in the documentationfor 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
注意:在 4.0 中,事件名称是user.login. 在 4.1 中,事件名称是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.
所以,您知道如何注册事件,但您可能想知道在哪里注册它们。别担心,这是一个常见的问题。不幸的是,这是一个很难回答的问题,因为您几乎可以在任何地方注册活动!但是,这里有一些提示。同样,与大多数其他引导代码一样,您可以在您的启动文件之一中注册事件,例如 app/start/global.php。
如果您的启动文件变得过于拥挤,您可以创建一个单独的 app/events.php 文件,该文件包含在启动文件中。这是一个简单的解决方案,可以将您的事件注册与其他引导程序完全分开。如果您更喜欢基于类的方法,您可以在服务提供者中注册您的事件。由于这些方法都不是本质上“正确的”,因此请根据应用程序的大小选择一种您觉得合适的方法。
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 thisdocumentation entry.
我个人的偏好是在服务提供商中注册它们,因为这会使它们保持隔离,对我来说是更“Laravel”的做事方式。如果您需要有关服务提供商的帮助,请参阅此文档条目。
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
回答by Dan H
In Laravel 5.2+ (tested on 5.6 as well) the process is a little different. First, add your listener to the EventServiceProvider:
在 Laravel 5.2+(也在 5.6 上测试)中,过程有点不同。首先,将您的侦听器添加到 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:
这将在 app/Listeners 中创建一个新的侦听器。现在编辑您的侦听器以触摸用户表中 last_login 列中的日期:
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:
确保由 Artisan 创建的 Listener 在顶部具有正确的命名空间。对于这种特殊情况,您的 Listener 中正确的命名空间应该是:
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
回答by Rafael Xavier
Just add this code below into your App\Http\Controllers\Auth\LoginController.php
只需将此代码添加到您的 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();
}
回答by jor
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.
这对我有用。

