From a9e89a685a09087d4148f1b50a0dea0d3e7ed5f1 Mon Sep 17 00:00:00 2001 From: Tim Kolijn Date: Tue, 2 Jun 2026 20:23:37 +0200 Subject: [PATCH 1/2] Add migration to clean-up orphaned certificate pivot rows --- ...leanup_orphaned_certificate_pivot_rows.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 database/migrations/2026_06_02_182050_cleanup_orphaned_certificate_pivot_rows.php diff --git a/database/migrations/2026_06_02_182050_cleanup_orphaned_certificate_pivot_rows.php b/database/migrations/2026_06_02_182050_cleanup_orphaned_certificate_pivot_rows.php new file mode 100644 index 00000000..0c6aec9e --- /dev/null +++ b/database/migrations/2026_06_02_182050_cleanup_orphaned_certificate_pivot_rows.php @@ -0,0 +1,29 @@ + + where('certificate_id', function ($query) { + $query->select('id')->from('certificates')->whereNotNull('deleted_at'); + })->delete(); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + // + } +}; From 191e5d3972589c82f73c08fea677b555acbc4884 Mon Sep 17 00:00:00 2001 From: Tim Kolijn Date: Tue, 2 Jun 2026 20:35:00 +0200 Subject: [PATCH 2/2] Add logic to detach users when a role is deleted. --- app/Certificate.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/Certificate.php b/app/Certificate.php index bb815ba1..6c4bb422 100644 --- a/app/Certificate.php +++ b/app/Certificate.php @@ -12,4 +12,14 @@ class Certificate extends Model 'name', 'abbreviation', ]; + + public static function booted(){ + static::deleted(function($certificate){ + $certificate->users()->detach(); + }); + } + + public function users(){ + return $this->belongsToMany(User::class); + } }