1
0
Fork 0

更新 app/Models/Keyword.php

panliang 2023-12-01 09:42:44 +08:00
parent 9ace922048
commit c7008bf4d6
1 changed files with 31 additions and 0 deletions

View File

@ -21,4 +21,35 @@ class Keyword extends Model
'options' => 'array', 'options' => 'array',
'image' => StorageFile::class, '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();
});
}
} }