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

ilgenfritz's avatar

Laravel Permissions only use roles

Hello, we have a simple app to create tickets and we're using the "spatie/laravel-permissions" package. Now we want to disallow certain functions using roles or show only certain functions if a certain role is given. But we want the Super Admin to always ignore/bypass this check.

We have put the following code in the AuthServiceProvider:

Gate::before(function ($user) {
    return $user->hasRole('admin') ? true : null;
});

But when i use this code in my Blade file, i still can't see the required function even when i have the role "admin".

@hasrole('processing')
    test
@endhasrole

I want the "admin" role to bypass all "@hasrole" checks.

What am i doing wrong?

0 likes
5 replies
tykus's avatar

AFAIK, hasrole is literally checking that the authenticated user has the given Role; it does not use the Gate, so the before gate is never checked in this context. The @can directive (which checks permissions) would work as you are expecting.

You are working against the recommended approach for the package which suggests that you should be authorizing against permissions, not roles.

ilgenfritz's avatar

@tykus I have read the linked recommendation, but we only have a handful of roles without any underlying permissions.

Is there anyway to build this with @hasrole or an alternative without creating permissions?

tykus's avatar
tykus
Best Answer
Level 104

@ilgenfritz make your application’s roles as permissions.

TBH based on what you’re describing, the package seems to be overkill. I would simply define the roles as a set of Gates in your circumstances.

1 like

Please or to participate in this conversation.