Laravel-上次登录日期和时间时间戳 [英] Laravel - last login date and time timestamp

查看:78
本文介绍了Laravel-上次登录日期和时间时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有自动插入的字段(例如 updated_at created_at ),但我想知道是否有一种雄辩的方法 timestamp()还是Laravel产生时间戳的方式?

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.

推荐答案

您可能会看到auth.login事件并更新用户的上次登录时间.请参阅文档中的第一个示例,以了解事件可以完全完成您要执行的操作.

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();
});

注意:在4.0中,事件名称为user.login.在4.1中,事件名称为auth.login

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

放置事件监听器的位置完全取决于您.如果您阅读链接到文档页面,则会显示:

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

因此,您知道如何注册事件,但是您可能想知道在哪里注册 注册他们.别担心,这是一个常见问题.很遗憾, 这是一个很难回答的问题,因为您可以注册一个活动 几乎在任何地方!但是,这里有一些技巧.再次,像大多数其他 自举代码,您可以在一个启动文件中注册事件 例如app/start/global.php.

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.

如果开始文件太拥挤,则可以创建一个 起始文件中包含的单独的app/events.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.

但是,如果您真的只是想尽快启动并运行某件东西,那么在app/start/global.php

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

这篇关于Laravel-上次登录日期和时间时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆