亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 164 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號