Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jorge_dev96's avatar

Add attributes to my auth()->user session

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;
        }

 }
```
0 likes
9 replies
vandan's avatar

@jorge_dev96 try this

public function getUsernameAttribute($username)
    {
        return \Auth::attempt(array('username' => $username));
    }
Snapey's avatar

This

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

is totally unnecessary since you don't change the field in any way !

add dd(auth()->user()) somewhere in your code and tell us what it says

1 like
jorge_dev96's avatar
User {#261 ▼
  #fillable: array:5 [▼
    0 => "name"
    1 => "username"
    2 => "email"
    3 => "password"
    4 => "profile_image"
  ]
  #hidden: array:2 [▼
    0 => "password"
    1 => "remember_token"
  ]
  #casts: array:1 [▶]
  #connection: "mysql"
  #table: "users"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:10 [▼
    "id" => 1
    "name" => "Marcia Bruen III"
    "username" => "rboehm"
    "email" => "[email protected]"
    "email_verified_at" => "2020-02-19 21:07:14"
    "password" => "yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi"
    "profile_image" => null
    "remember_token" => "VYnO7NoynsHlNnwoHgzHw0T0vCYHI4MU09K1QtGvw8sEKJo55cXlQ9j6kpLG"
    "created_at" => "2020-02-19 21:07:14"
    "updated_at" => "2020-02-19 21:07:14"
  ]
  #original: array:10 [▼
    "id" => 1
    "name" => "Marcia Bruen III"
    "username" => "rboehm"
    "email" => "[email protected]"
    "email_verified_at" => "2020-02-19 21:07:14"
    "password" => "yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi"
    "profile_image" => null
    "remember_token" => "VYnO7NoynsHlNnwoHgzHw0T0vCYHI4MU09K1QtGvw8sEKJo55cXlQ9j6kpLG"
    "created_at" => "2020-02-19 21:07:14"
    "updated_at" => "2020-02-19 21:07:14"
  ]
  #changes: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #visible: []
  #guarded: array:1 [▶]
  #rememberTokenName: "remember_token"
}
Snapey's avatar

Well its there, and no different to any other attributes (as expected). How are you trying to get username?

jorge_dev96's avatar

I have this in my controller

public function index(Request $request)
    {
        echo "<pre>";
        dd(auth()->user()->username);
        echo "</pre>";
        die();
}
jorge_dev96's avatar

It works with other attributes like auth()->user()->email but not with my custom attributes :c

jorge_dev96's avatar

I solved it I don't know how but I create separate migrations for my custom values and now it works witout this public function getUsernameAttribute() { return $this->username; }

Please or to participate in this conversation.