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

obink's avatar
Level 1

Implement Gate for User's role inside a User Table

I have this schema of User table

        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique()->nullable();
            $table->string('username')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('role')->default('kids');
            $table->tinyInteger('deleteStatus')->default('0');
            $table->rememberToken();
            $table->timestamps();
        });

and I think this project just has a single role for each users, so I'm trying to implement a gate for this peculiar role. Let Say a teacher and a student.

in my app User, I'm made a teacher function

public function hasRole($role)
    {
        if($this->where('role', $role)->first()){
            return true;
        }

        return false;
    }

in my AuthServiceProvider, I'm adding this

Gate::define('teacher', function($user){
return $user->hasRole('teacher');
});

and inside of users.index, I began testing it and it fails me.

@can('teacher')
<a href="{{route('users.edit', $user->id)}} " class="btn-sm btn-primary">test</a>
@endcan

I know the other method, using a Role table and make it many to many relationships. But I think that won't be necessary.

how to do this, please help.

0 likes
3 replies
rodrigo.pedra's avatar
Level 56

As the User model has a role column you should change your hasRole(...) method in your User model to:

public function hasRole($role)
{
    return $this->role === $role;
}

Your previous implementation would query for any record with a matching role, not testing that particular method.

Also, as since Laravel 6 one can define Gates for guest users, you should account for the $user parameter inside your gate callback to be null if no user is logged in.

Reference: https://laravel.com/docs/6.x/authorization#guest-users

To fix that you could use the optional(...) helper:

Gate::define('teacher', function($user){
    return optional($user)->hasRole('teacher');
});

Or test if the $user parameter is null before calling the ->hasRole(...) method:

Gate::define('teacher', function($user){
    if (!$user) {
         return false;
    }

    return $user->hasRole('teacher');
});

Hope it helps.

1 like
obink's avatar
Level 1

hi @rodrigo.pedra I just tried yours once and it worked like a charm. And luckily this project is in laravel 7

this Optional() thing is new to me.

thanks again.

1 like

Please or to participate in this conversation.