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); + } } 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 + { + // + } +};