From de6249216a24165cfe4ffc4b2baafc3c5f61e730 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Tue, 4 Nov 2025 16:47:21 +0100 Subject: [PATCH] Fix DeleteResourceMapping phpstan error, add comments --- .../Listeners/DeleteResourceMapping.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ResourceSyncing/Listeners/DeleteResourceMapping.php b/src/ResourceSyncing/Listeners/DeleteResourceMapping.php index cd836bb5..319fd043 100644 --- a/src/ResourceSyncing/Listeners/DeleteResourceMapping.php +++ b/src/ResourceSyncing/Listeners/DeleteResourceMapping.php @@ -12,6 +12,12 @@ use Stancl\Tenancy\ResourceSyncing\Events\SyncedResourceDeleted; use Stancl\Tenancy\ResourceSyncing\Syncable; use Stancl\Tenancy\ResourceSyncing\SyncMaster; +/** + * Deletes pivot records when a synced resource is deleted. + * + * If a SyncMaster (central resource) is deleted, all pivot records for that resource are deleted. + * If a Syncable (tenant resource) is deleted, only delete the pivot record for that tenant. + */ class DeleteResourceMapping extends QueueableListener { public static bool $shouldQueue = false; @@ -28,7 +34,9 @@ class DeleteResourceMapping extends QueueableListener // or the central resource was deleted using forceDelete() if ($event->forceDelete || ! in_array(SoftDeletes::class, class_uses_recursive($centralResource::class), true)) { Pivot::withoutEvents(function () use ($centralResource, $event) { - $centralResource?->tenants()->detach($event->tenant); + // $event->tenant is null when the deleted resource is a SyncMaster - all mappings are deleted in that case + // When $event->tenant is not null (= a Syncable was deleted), only delete the mapping for that tenant + $centralResource->tenants()->detach($event->tenant); }); } }