From e14956db98c0693bfc8542d25a03ca4c6762deeb Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 25 Apr 2024 09:01:58 -0400 Subject: [PATCH] Cleanup after model is deleted --- behaviors/TranslatableModel.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/behaviors/TranslatableModel.php b/behaviors/TranslatableModel.php index c47f45fc..14f4c109 100755 --- a/behaviors/TranslatableModel.php +++ b/behaviors/TranslatableModel.php @@ -26,8 +26,20 @@ public function __construct($model) $model->morphMany['translations'] = [ 'Winter\Translate\Models\Attribute', - 'name' => 'model' + 'name' => 'model', + 'delete' => true, ]; + + $this->model->bindEvent('model.afterDelete', [$this, 'afterModelDelete']); + } + + public function afterModelDelete() + { + // delete translation indexes for this record + Db::table('winter_translate_indexes') + ->where('model_id', $this->model->getKey()) + ->where('model_type', $this->getClass()) + ->delete(); } /**