Laravel-permission: Roles & Permissions are not cached

Created on 19 Feb 2018  路  4Comments  路  Source: spatie/laravel-permission

Hello,

I have installed the package successfully and every other feature works as it is expected to.

While using debugbar to check the number of queries run, the below query is executed in almost all pages.

select `roles`.*, `model_has_roles`.`model_id` as `pivot_model_id`, `model_has_roles`.`role_id` as `pivot_role_id` from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `model_has_roles`.`model_id` = '1' and `model_has_roles`.`model_type` = 'App\User'

/vendor/spatie/laravel-permission/src/Traits/HasRoles.php:213

The cache settings are configured in the app. How to get the cache working?

Most helpful comment

@andyunleashed Thanks for the suggestion.

@drbyte Will this be taken into consideration in the upcoming releases?

All 4 comments

I am having this problem also - causing some issues in a system where we return HTML rows for some services and there is a permission check on each row. Might return a thousand rows, and therefore tries to check the permission 1000 times.

Just to add to my previous - I think the caching part is easy to misread - it appears that the package caches all permissions, but not a user's specific permissions. So each ->can() or ->hasPermissionTo() checks each time.

To get around this in my app when doing the search I'm doing it once and then passing a boolean into blade to avoid having to check for each row being returned.

@andyunleashed wrote:

it appears that the package caches all permissions, but not a user's specific permissions

Yes, that is correct.

@andyunleashed Thanks for the suggestion.

@drbyte Will this be taken into consideration in the upcoming releases?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

devingray picture devingray  路  3Comments

tripex picture tripex  路  3Comments

feliperoan picture feliperoan  路  3Comments

MichalKrakow picture MichalKrakow  路  4Comments