laravel - How to add a custom attribute to Auth::user() when one logs in -


so, first hope title not misleading. please let me know if title fits question.

question. i'm using laravel 5.3 , i'm trying find way add attribute "role" auth::user() can access like

auth::user()->role.  

role not field in user table used authentication it's value calculate get. reason ask don't want calculate role every single time need value, instead want set when authentication succeeds reuse it.

any idea how can this?

or there way can persist value through out time user logged in can calculate once , reuse it?

the best way can achieve in user.php model:

<?php  namespace app;  use illuminate\notifications\notifiable; use illuminate\foundation\auth\user authenticatable;      class user extends authenticatable     {         use notifiable;          /**          * attributes mass assignable.          *          * @var array          */         protected $fillable = [             'name', 'email', 'password',         ];          /**          * attributes should hidden arrays.          *          * @var array          */         protected $hidden = [             'password', 'remember_token','is_admin',         ];          protected function getrole(){             if(\session::has("role")){             return session::get("role");         }else{            //calculate , save role in session         }      }//end role  }//end class 

so check

auth::user()->getrole(); 

make sure save role in session after login , getrole , attach user() object , doesnt need middleware hope helps


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -