One solution is to have an explicit Role model in your application that defines the id, name in a sql table.
class User extends Model
{
public function role()
{
return $this->belongsTo(Role::class);
}
}
Then you can consume it using using $user->role->name.
If you want to hard code the roles you could use a model getter for a role attribute.
class User extends Model
{
protected $roles = [
'0' => 'Member',
'1' => 'Super Admin',
'2' => 'Admin',
...
];
public function getRoleAttribute()
{
return $this->roles[$this->role_id];
}
}
Then you can consume it using $user->role.