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

daugaard47's avatar

Refactor If statement

I'm displaying my users in my admin section and have created some Roles.

I'm storing the Roles as integers, so when I output the users role they show as numbers. I created a super long If statement like this to output the correct role name.

What's / Is there a better way to do this?

(FYI this is in my blade file)

@if($user->role_id === 0)
    Member
@elseif($user->role_id === 1)
    Super Admin
@elseif($user->role_id === 2)
    Admin
@elseif($user->role_id === 3)
    Moderator
@elseif($user->role_id === 4)
    Editor
@elseif($user->role_id === 5)
    Role X
@elseif($user->role_id === 6)
    Role Y
@elseif($user->role_id === 7)
    Role Z
@endif

Here is my basic controller function: (If Needed)

    public function adminUserIndex(){
        $users = User::orderBy('id', 'desc')->get();
        return view('admin.user.index',compact('users'));
    }

0 likes
4 replies
lvismer's avatar
lvismer
Best Answer
Level 32

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.

1 like
YeZawHein's avatar

Yikes! You haven't name in roles table?

{{ $user->role->name }}
daugaard47's avatar

Should have mentioned... I have a Roles Table. Here is the Role Model:

class Role extends Model
{
    protected $table = 'roles';
    protected $fillable = [
        'name',
        'description',
            
    ];

    public function users()
    {
        return $this->hasMany('App\User', 'role_id', 'id');
    }
}

Here is the User Model

    public function role()
    {
        return $this->hasOne('App\Role', 'id', 'role_id');
    }

    public function hasRole($roles)
    {
        $this->have_role = $this->getUserRole();
        // Check if the user is a root account
        if($this->have_role->name == 'Root') {
            return true;
        }
        if(is_array($roles)){
            foreach($roles as $need_role){
                if($this->checkIfUserHasRole($need_role)) {
                    return true;
                }
            }
        } else{
            return $this->checkIfUserHasRole($roles);
        }
        return false;
    }
    
    private function getUserRole()
    {
        return $this->role()->getResults();
    }
    
    private function checkIfUserHasRole($need_role)
    {
        return (strtolower($need_role)==strtolower($this->have_role->name)) ? true : false;
    }

daugaard47's avatar

@lvismer Thank you! Both solutions worked. I opted for your first one. I appreciate your time.

Please or to participate in this conversation.