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

hoanbv's avatar

Laravel/nova with policy and spatie/laravel-permission

I'm facing a performance issue when using laravel policy with spatie/laravel-permission in laravel/nova.

I have 2 models as follows: Apartment and Owner with the following relationship Apartment belongsTo Owner

When loading the resource index page of the apartment model, I want to display the Owner of the Apartment.

Problem: The policy viewAny of the Apartment model is called many times when you want to display fields that have a belongsTo relationship (in this case, Owner)

How can I reduce the number of calls to policy viewAny?

Thanks for reading the post.

0 likes
1 reply
DavidPetrov's avatar

Best you can do if you really need the whole owner instance inside the policy is to eager load the owner relationship on your apartment resource. Other than that, if you need a simple comparison, you can compare the owner_id foreign key... or better yet, since it's a belongsTo relationship, compare the models via the is() method on the relationship instance... like that:

return $apartment->owner()->is($user);

This won't execute a query.

Please or to participate in this conversation.