ItemCategory.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Module\GameItems\Models;
  3. use Dcat\Admin\Traits\ModelTree;
  4. use Illuminate\Database\Eloquent\Relations\HasMany;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use UCore\ModelCore;
  7. /**
  8. * 物品分类
  9. *
  10. * field start
  11. * @property int $id 分类ID,主键
  12. * @property string $name 分类名称
  13. * @property string $code 分类编码(唯一)
  14. * @property string $icon 分类图标
  15. * @property int $sort 排序权重
  16. * @property int $parent_id 父分类ID(可为空,用于实现分类层级)
  17. * @property \Carbon\Carbon $created_at 创建时间
  18. * @property \Carbon\Carbon $updated_at 更新时间
  19. * field end
  20. */
  21. class ItemCategory extends ModelCore
  22. {
  23. use ModelTree;
  24. /**
  25. * 与模型关联的表名
  26. *
  27. * @var string
  28. */
  29. protected $table = 'item_categories';
  30. protected $titleColumn = 'name';
  31. protected $orderColumn = 'sort';
  32. protected $parentColumn = 'parent_id';
  33. // attrlist start
  34. protected $fillable = [
  35. 'id',
  36. 'name',
  37. 'code',
  38. 'icon',
  39. 'sort',
  40. 'parent_id',
  41. ];
  42. // attrlist end
  43. /**
  44. * 获取该分类下的所有物品
  45. *
  46. * @return HasMany
  47. */
  48. public function items(): HasMany
  49. {
  50. return $this->hasMany(Item::class, 'category_id', 'id');
  51. }
  52. /**
  53. * 获取父分类
  54. *
  55. * @return BelongsTo
  56. */
  57. public function parent(): BelongsTo
  58. {
  59. return $this->belongsTo(ItemCategory::class, 'parent_id');
  60. }
  61. /**
  62. * 获取子分类
  63. *
  64. * @return HasMany
  65. */
  66. public function children(): HasMany
  67. {
  68. return $this->hasMany(ItemCategory::class, 'parent_id');
  69. }
  70. }