ConsumableCategory.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Models;
  3. use Dcat\Admin\Traits\HasDateTimeFormatter;
  4. use Dcat\Admin\Traits\ModelTree;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\HasOne;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. /**
  9. * @property string name
  10. * @property string|null description
  11. *
  12. * @method static pluck(string $string, string $string1)
  13. * @method static where(string $string, string $string1, string $string2)
  14. */
  15. class ConsumableCategory extends Model
  16. {
  17. use HasDateTimeFormatter;
  18. use SoftDeletes;
  19. use ModelTree;
  20. protected $table = 'consumable_categories';
  21. protected $titleColumn = 'name';
  22. /**
  23. * 设备分类有一个父级分类.
  24. *
  25. * @return HasOne
  26. */
  27. public function parent(): HasOne
  28. {
  29. return $this->hasOne(self::class, 'id', 'parent_id');
  30. }
  31. /**
  32. * 如果数据库内现存数据是空的,那么对这个字段访问修饰,返回0
  33. * 因为模型树排序一定要有parent_id的值
  34. *
  35. * @param $parent_id
  36. *
  37. * @return int
  38. */
  39. public function getParentIdAttribute($parent_id): int
  40. {
  41. if (empty($parent_id)) {
  42. return 0;
  43. }
  44. return $parent_id;
  45. }
  46. }