Laravel问题上次登录,时间标记在每一行中

问题描述 投票:1回答:3

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

enter image description here

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;

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        'Illuminate\Auth\Events\Login' => [
            'App\Listeners\UpdateLastSignInAt',
        ],

        'Illuminate\Auth\Events\Registered' => [
            'App\Listeners\LogRegisteredUser',
        ],

    ];
    public function boot()
    {
        parent::boot();
        //
    }
}
php laravel laravel-5.8
3个回答
0
投票

尝试这样的事情

    public function handle(Login $event)
    {
        Auth::user()->LAST_LOGIN = Carbon::now();
        Auth::user()->save();
    }

0
投票

为什么不覆盖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);
}

0
投票

我的登录控制器

    public function login(){ 
    if(Auth::attempt(['USER_NAME' => request('USER_NAME'), 'password' => request('PASSWORD')])){ 
        $user = Auth::user(); 
        return response()->json(['success' => 'success'], $this-> successStatus);  
    } 
    else{ 
        return response()->json(['error'=>'Unauthorised'], 401); 
    } 
}
© www.soinside.com 2019 - 2024. All rights reserved.