记录用户的最后一次登录时间戳和 ip laravel 8 fortify

Record last login timestamp and ip of a user laravel 8 fortify

我目前正在记录用户的上次登录时间戳和登录用户 table 的 ip。我在 laravel 7 中的身份验证控制器登录功能中执行了此操作。 像这样:

            $user->last_login = Carbon::now()->toDateTimeString();
            $user->last_login_ip = $request->getClientIp();
            $user->save();

但我当前的项目使用 laravel fortify 包。我还在学习这个包。记录用户登录时间戳和 ip 的最佳方法是什么。 ?

谢谢

现在才想通,我用了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
        ],
/** ***/

    ];

  
}