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?
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: