lundi 23 septembre 2019

Laravel 5.5 - How can I add a custom value to Auth::user() after successfully login

I want to add a custom value (role) to Auth::user() after successful login.

I added following code in \App\User

protected $attributes = ['role'];
protected $appends = ['role'];

public function getRoleAttribute() 
{
    return $this->attributes['role'];
}    

public function setRoleAttribute($value)
{
    $this->attributes['role'] = strtolower($value);
}

and I set role from Auth\LoginController

function authenticated(Request $request, $user)
{ 
     //I find $position based on some calculation
     if($position == 1)
     {
         Auth::user()->role = 'director';
         return redirect('supervisors');
     }

}

I want to get role in different pages like $role = Auth::user()->role; (but this returns error)

how can I achieve this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire