logging - Laravel 5.4 listener to save users login -


i want save via event listener users login , have following code in custom listener. when try id of user returns each time null. approach how should proceed or on wrong track?

<?php  namespace app\listeners;  use app\user; use app\loginlog; use illuminate\queue\interactswithqueue; use illuminate\contracts\queue\shouldqueue; use event;  class logsuccessfullogin {       protected $user;       /**      * create event listener.      *      * @return void      */     public function __construct(user $user)     {         $this->user = $user;     }      /**      * handle event.      *      * @param  illuminateautheventslogin  $event      * @return void      */     public function handle($event)     {          var_dump($this->user->account_id);         die;             $login  = new loginlog();              $login->ip = \request::ip();             $login->notiz = 'login';             $login->login_date = \carbon\carbon::now();             $login->user = $this->user->account_id;              $login->save();        } } 

i think should try user directly event. :

namespace app\listeners;  use illuminate\auth\events\login; use illuminate\http\request;  class logsuccessfullogin {     /**      * create event listener.      *      * @param  request  $request      * @return void      */     public function __construct(request $request)     {         $this->request = $request;     }      /**      * handle event.      *      * @param  login  $event      * @return void      */     public function handle(login $event)     {         $user = $event->user;         $user->last_login_at = date('y-m-d h:i:s');         $user->last_login_ip = $this->request->ip();         $user->save();     } } 

this tutorial : https://stevenwestmoreland.com/2017/03/recording-last-login-information-using-laravel-events.html hope helps you


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -