gpt4 book ai didi

php - Laravel 5.2 登录事件处理

转载 作者:可可西里 更新时间:2023-11-01 00:00:05 26 4
gpt4 key购买 nike

在数据库中,我有一个包含列 last_login_at 的表 users。每次当一些用户登录时 - 我想更新 last_login_at

因此,我创建了 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();
}
}

在 app/Providers/EventServiceProvider 中:

    protected $listen = [
'auth.login' => [
'App\Listeners\UpdateLastLoginOnLogin',
],
];

但这不起作用,事件未处理。这里已经提到了同样的问题:EventServiceProvider mapping for Laravel 5.2 login但没有解决方案。我试过这样做:

...

use Illuminate\Auth\Events\Login;

class UpdateLastLoginOnLogin
{
public function handle(Login $event)
{
$event->user->last_login_at = Carbon::now();
$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-compiled 没有解决问题。

编辑:有关其他详细信息,这里有一个指向实际上完全相同(以相同方式完成)的项目的链接:https://github.com/tutsplus/build-a-cms-with-laravel

最佳答案

你就快完成了,只是还有一些变化,用于身份验证的事件和监听器在 Laravel 5.2 中发生了一些变化:UpdateLastLoginOnLogin 中的 handle 方法应该只有一个事件作为参数

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

对于EventServiceProvider,您可以像这样指定监听器:

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

关于php - Laravel 5.2 登录事件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35295062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com