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

onairmarc's avatar

Spatie Permissions Question

Hi All,

I’m wondering if the Spatie Permissions package keeps each permission check in memory for the lifetime of the current request. I’m trying to avoid calling the database every time I check permissions if I’ve already evaluated the same permission earlier in the request. I’m using Filament to build an admin panel for a custom CMS and the current package I’m using calls the database for every single row in a table despite their docs saying that permission checks are cached (I’m currently using Laratrust for those who are curious).

I’ve spent countless hours trying to fix this issue and saw some minor improvements but at the cost of some compromises that just won’t work for our use case.

Given that the Spatie package has a first party Filament plugin, I’m seriously considering making this change. A change needs to be made regardless, but knowing as much as I can prior to switching things around would be amazing!

Thanks!

0 likes
4 replies
LaryAI's avatar
Level 58

The Spatie Laravel Permission package does indeed cache the permission checks for the duration of the request to avoid unnecessary database calls. Once a permission or role check is made, the result is stored in memory, and subsequent checks for the same permission or role within the same request will not hit the database again.

Here's how it works:

  1. When you check a permission or role for the first time in a request, the package retrieves the necessary data from the database.
  2. The retrieved data is then stored in the cache.
  3. Subsequent checks for the same permission or role will use the cached data instead of querying the database again.

This caching mechanism is built into the package and does not require any additional configuration. However, you can configure the cache duration and other cache-related settings in the config/permission.php configuration file if needed.

Here's an example of how you might check permissions using the Spatie Permission package:

use Spatie\Permission\Models\Permission;

// Checking a permission for the first time in a request
if (auth()->user()->hasPermissionTo('edit articles')) {
    // This will retrieve and cache the permission check
}

// Checking the same permission again in the same request
if (auth()->user()->hasPermissionTo('edit articles')) {
    // This will use the cached permission check and not hit the database
}

If you are experiencing performance issues with your current setup and are considering switching to the Spatie Permission package, it is worth noting that the package is well-maintained and widely used in the Laravel community. Additionally, since you mentioned that there is a first-party Filament plugin for the Spatie Permission package, it should integrate smoothly with your admin panel.

Before making the switch, you may want to review the documentation for the Spatie Permission package and the Filament plugin to ensure that it meets all your requirements and to understand how to properly implement it in your application.

1 like
rvdiwas's avatar

yes it does cache all the permission checks. By default it is cached for 24 hours, you can change that by changing config('permission.cache.expiration_time')

1 like
onairmarc's avatar

@rvdiwas I am aware that it does do caching using the cache driver. What I'm wondering is if I check if a user can('edit users') and then I make that same check later within the same request, does the check make multiple calls to the database or keep the result of the permission check in memory for the remainder of the request resulting in only a single database call regardless of how many permission checks are done.

I'm hoping I explained that clearly enought.

Please or to participate in this conversation.