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

Joyal

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?

Joyal

I found a solution myself.

Since I have to use the 'role' value in different controllers and there is no column in database table to keep the value I had to use session to store the value.

I set role value in session when user is authenticated at Auth\LoginController

function authenticated(Request $request, $user)
{
    if(xxxxx)
    {
        if(xxxx)
        {
            Auth::user()->role = 'director';
        }
        else
        {
            Auth::user()->role = 'manager';              
        }
    }
}

In App\User

public function getRoleAttribute() 
{
    return Session::get('role', null);
}

public function setRoleAttribute($value)
{
    Session::put('role', $value);
}

Now I can access the role value from anywhere using the following code

Auth::user()->role

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add custom value to Auth::user() in Laravel

Laravel 5: How to add Auth::user()->id through the constructor ?

How can I add a user to login as a user and not as a root Yocto project sama5d27 board

How to login user with Laravel 5

Laravel 5: The difference between Auth::guard($this->getGuard())->login($user); and auth()->login($user);

laravel 5 /auth/login not found

laravel 5 redirect user after login based on user's role

Can't login to django admin after successfully creating a super user with a custom user model

In Django, after a login how can I detect which auth backend authenticated the user?

Laravel 5 custom auth attempt

Using Laravel 5, how can I find out if a Mailgun email was sent successfully?

Add the user role to the jwt, laravel 5 jwt-auth

How can I use my custom class in a view on Laravel 5

How can I add external class in Laravel 5

How can I add default scope / conditions on a relationship in Laravel 5?

Laravel 5 : Does Auth::user() query the database everytime I use it?

Auth::user() not found after login laravel 6

LARAVEL5 Custom login

How can i decode hash value in laravel 5?

How I can add more data to Laravel's auth()->user() object + it's relationships?

Unable to login with basic auth in Laravel 5

Laravel 5 Auth - Change Login Route

How to share Auth::user among all views in Laravel 5?

Laravel 5 new auth: Get current user and how to implement roles?

How can I create admin user accounts that can register non-admin user accounts in Laravel 5?

How can I use Facebook login in my app and move to next page/view controller after successfully logIn?

How use the hash in my custom login laravel5

How can I add a custom method to auth_user model class? I need to return a calculated field along with results

Laravel 5: How to store extra attributes to user login session