That is because it returns the whole array and not the color.
If you are using php8 you can do something like this
return match($this->role) {
'super-admin' => 'primary',
'admin' => 'info',
default => 'warning'
};
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
how can i get attributes from a relationship model this works when i get roles in a table
public function getRoleColorAttribute()
{
# code...
return [
'super-admin' => 'primary',
'admin' => 'info',
][$this->name] ?? 'warning';
}
the the role color doesn't work when its in relationship of users
<td>
@foreach ($user->roles as $role)
<a href="#" class="badge badge-light-{{$role->role_color}}">
{{ $role->name }}
</a>
@endforeach
</td>
That is because it returns the whole array and not the color.
If you are using php8 you can do something like this
return match($this->role) {
'super-admin' => 'primary',
'admin' => 'info',
default => 'warning'
};
Please or to participate in this conversation.