| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace App\Module\GameItems\Models;
- use Dcat\Admin\Traits\ModelTree;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use UCore\ModelCore;
- /**
- * 物品分类
- *
- * field start
- * @property int $id 分类ID,主键
- * @property string $name 分类名称
- * @property string $code 分类编码(唯一)
- * @property string $icon 分类图标
- * @property int $sort 排序权重
- * @property int $parent_id 父分类ID(可为空,用于实现分类层级)
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class ItemCategory extends ModelCore
- {
- use ModelTree;
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'item_categories';
- protected $titleColumn = 'name';
- protected $orderColumn = 'sort';
- protected $parentColumn = 'parent_id';
- // attrlist start
- protected $fillable = [
- 'id',
- 'name',
- 'code',
- 'icon',
- 'sort',
- 'parent_id',
- ];
- // attrlist end
- /**
- * 获取该分类下的所有物品
- *
- * @return HasMany
- */
- public function items(): HasMany
- {
- return $this->hasMany(Item::class, 'category_id', 'id');
- }
- /**
- * 获取父分类
- *
- * @return BelongsTo
- */
- public function parent(): BelongsTo
- {
- return $this->belongsTo(ItemCategory::class, 'parent_id');
- }
- /**
- * 获取子分类
- *
- * @return HasMany
- */
- public function children(): HasMany
- {
- return $this->hasMany(ItemCategory::class, 'parent_id');
- }
- }
|