From 4628ce85ac118d6169a8e594d53709a265e732cd Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 22 Jul 2021 18:56:16 +0200 Subject: [PATCH] app/Models/Character.php: Clear main character relationship if a main character is deleted. --- app/Models/Character.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/Models/Character.php b/app/Models/Character.php index b9d45ca..18184b6 100644 --- a/app/Models/Character.php +++ b/app/Models/Character.php @@ -26,6 +26,20 @@ class Character extends Model 'class' ]; + public static function boot() + { + parent::boot(); + + static::deleted(function($character) { + + // Unset main character. + if ($character->isMain()) { + $character->user->main_character()->dissociate(); + $character->user->save(); + } + }); + } + /** * Get the route key for the model. *