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

elo's avatar
Level 3

Issue with Laravel Policy

Hi, I am using laravel policies to authenticate user actions on an application backend which was built using laravel nova. I am also using spatie permissions and roles package https://spatie.be/docs/laravel-permission/v3/introduction in this project.

I have a UserTransactionPolicy which allows users with certain roles view a user's transactions and only those with a certain permission.

public function viewAny(User $user)
{
    return $user->hasRole('admin');
}

public function view(User $user, UserTransaction $userTransaction)
{
    return $user->hasRole('admin');
}

public function create(User $user)
{
    return $user->hasPermissionTo('create');
}

In AuthServiceProvider I have registered the policy along with other in $policies array like this

// other policies
'App\Models\UserTransaction'    => 'App\Policies\UserTransactionPolicy',
// more policies

On nova the authenticated user has admin role and permission to create and the first two policies work just fine but the create policy keeps throwing error

Sorry! You are not authorized to perform this action

The first two policies to view were added long ago and the create policy was only added today. So I commented out the policy in AuthServiceProvider to be sure that the new changes are actually taking effect but to my surprise, I still get the same error even though now I see all nova action buttons.

Is there away to refresh the policy list just incase is some issue with caching? Or is there something I haven't done right?

0 likes
4 replies
deansatch's avatar

I am sure I remember reading something in the spatie docs about avoiding naming a permission the same as a policy, so if you have a permission called “create” and a policy of the same name it will cause issues

elo's avatar
Level 3

In this case its the same error when I use $user->hasRole('admin'), I already tried doing that. It has to be something else

elo's avatar
Level 3

So I pushed my code to my staging server and experienced same thing as I did on my local system. But I pushed same code to production and works perfectly. Now I wouldn't want to do this as its best to always test code before pushing to production.

What do you guys think could be the reason for this?

UsmanBasharmal's avatar

You probably need to reset the cache of role and permission by running below command in the terminal as said in spatie doc

    php artisan permission:cache-reset

Please or to participate in this conversation.