现在才想通,我使用了 Laravel 登录事件。和它的工作。
//class event
<?php
namespace App\Events;
use Illuminate\Auth\Events\Login;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Carbon\Carbon;
class UpdateUserLastLoginDate
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param Login $event
* @return void
*/
public function handle(Login $event)
{
try {
$user = $event->user;
$user->last_login = Carbon::now()->toDateTimeString();
$user->last_login_ip = request()->getClientIp();
$user->save();
} catch (\Throwable $th) {
report($th);
}
}
}
//在事件服务提供者中
<?php
namespace App\Providers;
/** ***/
use Illuminate\Auth\Events\Login;
use App\Events\UpdateUserLastLoginDate;
/** ***/
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
/** ***/
Login::class => [
UpdateUserLastLoginDate::class
],
/** ***/
];
}