generated from panliang/owl-admin-starter
57 lines
1.6 KiB
PHP
57 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Casts\StorageFile;
|
|
use EloquentFilter\Filterable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use App\Traits\HasDateTimeFormatter;
|
|
use App\Traits\TreePath;
|
|
|
|
/**
|
|
* 字典表
|
|
*/
|
|
class Keyword extends Model
|
|
{
|
|
use Filterable, HasDateTimeFormatter, TreePath;
|
|
|
|
protected $fillable = ['id', 'name', 'key', 'value', 'parent_id', 'path', 'sort', 'level', 'options', 'image', 'images', 'description', 'content'];
|
|
|
|
protected $casts = [
|
|
'options' => 'array',
|
|
'images' => '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();
|
|
});
|
|
}
|
|
}
|