为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel问题上次登录,每行都标记时间

Laravel问题上次登录,每行都标记时间

PHP
慕容森 2022-06-17 15:51:32
我在上次登录时使用 Laravel 身份验证事件,但是当时间戳记到我在“LAST_LOGIN”列中的代码的每一行时出现问题我希望它只记录在登录用户中。但它看起来像登录时间将节省给每个用户。作为我附上的图片。app\Listeners\UpdateLastSignInAt    <?phpnamespace App\Listeners;use Illuminate\Auth\Events\Login;use Illuminate\Queue\InteractsWithQueue;use Illuminate\Contracts\Queue\ShouldQueue;use Carbon\Carbon;class UpdateLastSignInAt{    public function __construct()    {        //    }    public function handle(Login $event)    {        $event->user->LAST_LOGIN = Carbon::now();        $event->user->save();    }}\app\User.php --model--<?phpnamespace App;use Laravel\Passport\HasApiTokens;use Illuminate\Database\Eloquent\Model;use Illuminate\Notifications\Notifiable;use Illuminate\Foundation\Auth\User as Authenticatable;class User extends Authenticatable{    use HasApiTokens, Notifiable;    protected $table = 'SYSM_USERS';    public $timestamps = false;    protected $primaryKey = null;    public $incrementing = false;    protected $fillable = [        'ID', 'USER_NAME', 'PASSWORD', 'FIRST_NAME', 'LAST_NAME', 'DEPART_ID','E_MAIL','NOTE','STATUS','LAST_LOGIN','CREATED_BY','CREATED_DATE','UPDATED_BY','UPDATED_DATE'    ];    protected $hidden = [        'PASSWORD', 'remember_token',        ];        protected $dates = ['LAST_LOGIN','CREATED_DATE','UPDATED_DATE'];    public function getAuthPassword()    {        return $this->attributes['PASSWORD'];    }}\app\Providers\EventServiceProvider<?phpnamespace App\Providers;use Illuminate\Support\Facades\Event;use Illuminate\Auth\Events\Registered;use Illuminate\Auth\Listeners\SendEmailVerificationNotification;use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
查看完整描述

2 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

为什么不覆盖 authController 中的登录方法??????像这样


public function login(Request $request)

{

    $this->validateLogin($request);


    // If the class is using the ThrottlesLogins trait, we can automatically throttle

    // the login attempts for this application. We'll key this by the username and

    // the IP address of the client making these requests into this application.

    if (method_exists($this, 'hasTooManyLoginAttempts') &&

        $this->hasTooManyLoginAttempts($request)) {

        $this->fireLockoutEvent($request);


        return $this->sendLockoutResponse($request);

    }


    if ($this->attemptLogin($request)) {

        $user = auth()->guard('web')->user();

        //$user = auth()->guard('api')->user();

        $user->last_login = now()->toDateTimeString();

        $user->save();

        return $this->sendLoginResponse($request);

    }


    // If the login attempt was unsuccessful we will increment the number of attempts

    // to login and redirect the user back to the login form. Of course, when this

    // user surpasses their maximum number of attempts they will get locked out.

    $this->incrementLoginAttempts($request);


    return $this->sendFailedLoginResponse($request);

}


查看完整回答
反对 回复 2022-06-17
?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

最后我可以做到,我在函数句柄中使用 WHERE,具体来说


app\Listeners\UpdateLastSignInAt


        <?php


namespace App\Listeners;


use Illuminate\Auth\Events\Login;

use Illuminate\Queue\InteractsWithQueue;

use Illuminate\Contracts\Queue\ShouldQueue;

use Carbon\Carbon;


class UpdateLastSignInAt

{

    public function __construct()

    {

        //

    }

    public function handle(Login $event)

    {

        $date =  Carbon::now();

        $user = $event->user->ID;

        User::where('ID', $user)

              ->update(['LAST_LOGIN' => $date]);

    }

}


查看完整回答
反对 回复 2022-06-17
  • 2 回答
  • 0 关注
  • 177 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号