From c7008bf4d68bfb0b239e86dadac781e263714e0c Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Fri, 1 Dec 2023 09:42:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20app/Models/Keyword.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/Keyword.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/app/Models/Keyword.php b/app/Models/Keyword.php index e94bffd..f0b7242 100644 --- a/app/Models/Keyword.php +++ b/app/Models/Keyword.php @@ -21,4 +21,35 @@ class Keyword extends Model 'options' => 'array', 'image' => StorageFile::class, ]; + + protected static function booted(): void + { + static::creating(function (Model $model) { + if ($model->parent_id) { + $parent = static::query()->findOrFail($model->parent_id); + $model->path = $parent->path . $parent->id . '-'; + $model->level = $parent->level + 1; + } else { + $model->parent_id = 0; + $model->path = '-'; + $model->level = 1; + } + }); + + static::updating(function (Model $model) { + if ($model->parent_id) { + $parent = static::query()->findOrFail($model->parent_id); + $model->path = $parent->path . $parent->id . '-'; + $model->level = $parent->level + 1; + } else { + $model->parent_id = 0; + $model->path = '-'; + $model->level = 1; + } + }); + + static::deleting(function (Model $model) { + static::query()->allChildren($model->id)->delete(); + }); + } }