更新 app/Models/Keyword.php
parent
9ace922048
commit
c7008bf4d6
|
|
@ -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();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue