用户使用Laravel成功登录后,如何更新最后登录栏?

编程入门 行业动态 更新时间:2024-10-27 05:26:36
本文介绍了用户使用Laravel成功登录后,如何更新最后登录栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当用户成功登录时,我尝试更新最后一个登录列,我尝试将下面的代码放在LoginController中,但没有用,我也尝试了一个监听器,遵循这个答案,但是它没有用,什么也没发生,似乎监听器没有被执行.

I'm trying to update the last login column when the user successful login, I tried putting the code below in LoginController, but it didn't work, I've tried with a listener too, following this answer, but it didn't work, nothing ever happened, seems like the listener wasn't being executed.

DB::table('users') ->where('id', Auth::id()) ->update(['lastlogin' => Carbon::now()]);

我的laravel版本是5.4.12.我该如何运作?

My laravel version is 5.4.12. How can I get it working?

推荐答案

您可以订阅在用户成功通过身份验证后触发的Login事件.

You can subscribe to the Login event fired after a user has been successfully authenticated.

在app/providers/EventServiceProvider.php中,将事件和侦听器添加到$listen数组.

In app/providers/EventServiceProvider.php, add the event and listener to the $listen array.

protected $listen = [ // ... \Illuminate\Auth\Events\Login::class => [ \App\Listeners\LastLogin::class, ], // ... ];

在app/listeners/LastLogin.php

namespace App\Listeners; use App\Models\User; use Carbon\Carbon; use Illuminate\Auth\Events\Login; class LastLogin { /** * Handle the event. * * @param \Illuminate\Auth\Events\Login $event * @return void */ public function handle(Login $event) { // Update user last login date/time $event->user->update(['lastlogin' => Carbon::now()]); } }

更多推荐

用户使用Laravel成功登录后,如何更新最后登录栏?

本文发布于:2023-10-24 03:00:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522746.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!