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.