jorge_dev96
4 months ago
863
9
Laravel

Add attributes to my auth()->user session

Posted 4 months ago by jorge_dev96

Hi im trying to access to my attribute username auth->user->username but it says that username doesnt exist while username is actually in my model and in my database. I created a mutator in my model User, but idoesnt retrieve the username. It actually works with email, name, id atributters but not with username. What can I do :C

 <?php

 namespace ILink;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

    
class User extends Authenticatable
{
    use Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name','username', 'email', 'password','profile_image'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

       public function getUsernameAttribute()
       {
            return $this->username;
        }

 }
```

Please sign in or create an account to participate in this conversation.