猿问

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

我在上次登录时使用 Laravel 身份验证事件,但是当时间戳记到我在“LAST_LOGIN”列中的代码的每一行时出现问题我希望它只记录在登录用户中。但它看起来像登录时间将节省给每个用户。作为我附上的图片。

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)

    {

        $event->user->LAST_LOGIN = Carbon::now();

        $event->user->save();

    }

}

\app\User.php --model--


<?php


namespace 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


<?php


namespace 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;


慕容森
浏览 178回答 2
2回答

喵喔喔

为什么不覆盖 authController 中的登录方法??????像这样public function login(Request $request){&nbsp; &nbsp; $this->validateLogin($request);&nbsp; &nbsp; // If the class is using the ThrottlesLogins trait, we can automatically throttle&nbsp; &nbsp; // the login attempts for this application. We'll key this by the username and&nbsp; &nbsp; // the IP address of the client making these requests into this application.&nbsp; &nbsp; if (method_exists($this, 'hasTooManyLoginAttempts') &&&nbsp; &nbsp; &nbsp; &nbsp; $this->hasTooManyLoginAttempts($request)) {&nbsp; &nbsp; &nbsp; &nbsp; $this->fireLockoutEvent($request);&nbsp; &nbsp; &nbsp; &nbsp; return $this->sendLockoutResponse($request);&nbsp; &nbsp; }&nbsp; &nbsp; if ($this->attemptLogin($request)) {&nbsp; &nbsp; &nbsp; &nbsp; $user = auth()->guard('web')->user();&nbsp; &nbsp; &nbsp; &nbsp; //$user = auth()->guard('api')->user();&nbsp; &nbsp; &nbsp; &nbsp; $user->last_login = now()->toDateTimeString();&nbsp; &nbsp; &nbsp; &nbsp; $user->save();&nbsp; &nbsp; &nbsp; &nbsp; return $this->sendLoginResponse($request);&nbsp; &nbsp; }&nbsp; &nbsp; // If the login attempt was unsuccessful we will increment the number of attempts&nbsp; &nbsp; // to login and redirect the user back to the login form. Of course, when this&nbsp; &nbsp; // user surpasses their maximum number of attempts they will get locked out.&nbsp; &nbsp; $this->incrementLoginAttempts($request);&nbsp; &nbsp; return $this->sendFailedLoginResponse($request);}

qq_花开花谢_0

最后我可以做到,我在函数句柄中使用 WHERE,具体来说app\Listeners\UpdateLastSignInAt&nbsp; &nbsp; &nbsp; &nbsp; <?phpnamespace App\Listeners;use Illuminate\Auth\Events\Login;use Illuminate\Queue\InteractsWithQueue;use Illuminate\Contracts\Queue\ShouldQueue;use Carbon\Carbon;class UpdateLastSignInAt{&nbsp; &nbsp; public function __construct()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; }&nbsp; &nbsp; public function handle(Login $event)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $date =&nbsp; Carbon::now();&nbsp; &nbsp; &nbsp; &nbsp; $user = $event->user->ID;&nbsp; &nbsp; &nbsp; &nbsp; User::where('ID', $user)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->update(['LAST_LOGIN' => $date]);&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答