#Get Roles from Laravel Spatie/Permissions in Child Class

6 messages · Page 1 of 1 (latest)

karmic moon
#

Show code

wispy locust
#
{
    
    protected $table = 'users';
    
    protected $appends = [
        "role_names",
        "role_id",
    ];


    public function getRoleNamesAttribute()
    {
        return $this->roles()->get()->pluck("name")->toArray();
    }

    public function getRoleIdAttribute()
    {
        return $this->roles()->get()->pluck("id")->toArray();
    }
}```
#

this is the student-class which extends the user-class, I try to add two fields, role_names and role_ids

#
{
    use HasApiTokens;
    use HasFactory;
    use \Spatie\Permission\Traits\HasRoles;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;

    ...

    /**
     * The accessors to append to the model's array form.
     *
     * @var array<int, string>
     */
    protected $appends = [
        "role_names",
        "role_id",
    ];

    public function getRoleNamesAttribute()
    {
        return $this->roles()->get()->pluck("name")->toArray();
    }

    public function getRoleIdAttribute()
    {
        return $this->roles()->get()->pluck("id")->toArray();
    }

}```
#

it works in the User-Model but not in the Student-Model

#

in the student-model the fields role_names and role_ids are empty