From 4425a71556d91297e8bc52711c1bebcbcfde518c Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 2 Sep 2022 17:41:34 +0200 Subject: [PATCH] Suggest adding event listeners to `events()` in TenancyServiceProvider (#199) * Improve spatie/laravel-permission section * Don't imply that the events method is static --- source/docs/v3/integrations/spatie.blade.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/source/docs/v3/integrations/spatie.blade.md b/source/docs/v3/integrations/spatie.blade.md index a468d1c..3475a47 100644 --- a/source/docs/v3/integrations/spatie.blade.md +++ b/source/docs/v3/integrations/spatie.blade.md @@ -29,19 +29,23 @@ php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvid mv database/migrations/*_create_permission_tables.php database/migrations/tenant ``` -Then add this to your `TenancyServiceProvider::boot()`: +Next, add the following listeners to the `TenancyBootstrapped` and `TenancyEnded` events to `events()` in your `TenancyServiceProvider`: ```php -Event::listen(TenancyBootstrapped::class, function (TenancyBootstrapped $event) { - \Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache.tenant.' . $event->tenancy->tenant->id; -}); +Events\TenancyBootstrapped::class => [ + function (TenancyBootstrapped $event) { + \Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache.tenant.' . $event->tenancy->tenant->id; + } +], -Event::listen(TenancyEnded::class, function (TenancyEnded $event) { - \Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache'; -}); +Events\TenancyEnded::class => [ + function (TenancyEnded $event) { + \Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache'; + } +], ``` -The reason for this is that spatie/laravel-permission caches permissions & roles to save DB queries, which means that we need to separate the permission cache by tenant. +The reason for this is that spatie/laravel-permission caches permissions & roles to save DB queries, which means that we need to separate the permission cache by tenant. We also need to reset the cache key when tenancy ends so that the tenant's cache key isn't used in the central app. ## **laravel-medialibrary** {#laravel-medialibrary}