Laravel 5.2登录事件处理 [英] Laravel 5.2 Login Event Handling
问题描述
在数据库中,我有一个表用户的列为last_login_at。每次当某些用户登录时 - 我想开始 last_login_at 。
所以,我创建了app / Listeners / UpdateLastLoginOnLogin.php :
命名空间App \Listeners;
使用Carbon\\Carbon;
class UpdateLastLoginOnLogin
{
public function handle($ user,$ remember)
{
$ user-> last_login_at = Carbon :: now );
$ user-> save();
}
}
在app / Providers / EventServiceProvider:
protected $ listen = [
'auth.login'=> [
'App\Listeners\UpdateLastLoginOnLogin',
],
];
但是这不起作用,事件不处理。这里已经提到了同样的问题: Laravel 5.2登录的EventServiceProvider映射但没有解决方案。我试图这样做:
...
使用Illuminate\Auth\Events\Login;
class UpdateLastLoginOnLogin
{
public function handle(Login $ event)
{
$ event-> user-> last_login_at = Carbon ::现在();
$ event-> user-> save();
}
}
和:
protected $ listen = [
'Illuminate\Auth\Events\Login'=> [
'App\Listeners\UpdateLastLoginOnLogin',
],
];
但不行。
另外,我检查了这一点: https:/ /laracasts.com/discuss/channels/general-discussion/login-event-handling-in-laravel-5 ,但是 php artiasn clear-compiler 没有解决问题。 p>
编辑:对于其他详细信息,这里与项目的链接实际上完全相同(它以相同的方式完成): https://github.com/tutsplus/build-a-cms-with-laravel
你几乎在那里,只是一些更改,Laravel的身份验证事件和侦听器有所改变5.2:
UpdateLastLoginOnLogin 中的句柄方法应该只有一个事件作为参数
命名空间App \Listeners;
使用Carbon\\Carbon;
使用Auth;
class UpdateLastLoginOnLogin
{
public function handle($ event)
{
$ user = Auth :: user();
$ user-> last_login_at = Carbon :: now();
$ user-> save();
}
}
而对于 EventServiceProvider 你指定这样的听众:
protected $ listen = [
'Illuminate\Auth\Events\登录'=> [
'App \Listeners\UpdateLastLoginOnLogin @ handle',
],
];
In the database I have a table users with column last_login_at. Everytime when some user logs in - I want to uptade last_login_at.
So, I created app/Listeners/UpdateLastLoginOnLogin.php:
namespace App\Listeners;
use Carbon\Carbon;
class UpdateLastLoginOnLogin
{
public function handle($user, $remember)
{
$user->last_login_at = Carbon::now();
$user->save();
}
}
In app/Providers/EventServiceProvider:
protected $listen = [
'auth.login' => [
'App\Listeners\UpdateLastLoginOnLogin',
],
];
BUT this doesn't work, event is not handled. The same problem has already been mentioned here: EventServiceProvider mapping for Laravel 5.2 login but without solution. I have tried to do like this:
...
use Illuminate\Auth\Events\Login;
class UpdateLastLoginOnLogin
{
public function handle(Login $event)
{
$event->user->last_login_at = Carbon::now();
$event->user->save();
}
}
and:
protected $listen = [
'Illuminate\Auth\Events\Login' => [
'App\Listeners\UpdateLastLoginOnLogin',
],
];
But it doesn't work.
Also, I checked this: https://laracasts.com/discuss/channels/general-discussion/login-event-handling-in-laravel-5 but php artiasn clear-compiled didn't solve the problem.
EDIT: FOR OTHER DETAILS, HERE'S A LINK TO THE PROJECT which is actually exactly the same (it is done in the same way): https://github.com/tutsplus/build-a-cms-with-laravel
You are almost there, just a few changes more, Events and Listeners for authentication have changed a little in Laravel 5.2: the handle method in UpdateLastLoginOnLogin should have just an event as parameter
namespace App\Listeners;
use Carbon\Carbon;
use Auth;
class UpdateLastLoginOnLogin
{
public function handle($event)
{
$user = Auth::user();
$user->last_login_at = Carbon::now();
$user->save();
}
}
And for the EventServiceProvider you specify the listeners like this :
protected $listen = [
'Illuminate\Auth\Events\Login' => [
'App\Listeners\UpdateLastLoginOnLogin@handle',
],
];
这篇关于Laravel 5.2登录事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!