login event handling in laravel 5

后端 未结 8 2521
温柔的废话
温柔的废话 2020-12-05 11:46

i am trying to hook to the login even in my L5 app to set last login time and IP address. i can make it work with the following:

Event::listen(\'auth.login\'         


        
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 11:59

    For 5.2 something like this

    in Listeners:

    use Carbon\Carbon;
    use Illuminate\Auth\Events\Login;
    
    class UpdateLastLoginWithIp
    {
        public function handle(Login $event)
        {
            $event->user->last_login_at = Carbon::now();
            $event->user->last_login_ip = Request::getClientIp()
            $event->user->save();
        }
    }
    

    In EventServiceProvider.php :

    protected $listen = [
            'Illuminate\Auth\Events\Login' => [
                'City\Listeners\UpdateLastLoginWithIp',
            ],
        ];
    

提交回复
热议问题