You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB

6 years ago
6 years ago
6 years ago
  1. <?php
  2. namespace App\Model\v3;
  3. use App\Model\Model;
  4. use App\Service\v3\Interfaces\AttachmentServiceInterface;
  5. use Hyperf\Database\Model\Builder;
  6. use Hyperf\Database\Model\SoftDeletes;
  7. use Hyperf\Di\Annotation\Inject;
  8. class Category extends Model
  9. {
  10. use SoftDeletes;
  11. /**
  12. * @Inject
  13. * @var AttachmentServiceInterface
  14. */
  15. protected $attachmentService;
  16. protected $table = 'lanzu_category';
  17. protected $appends = [
  18. 'goods_types',
  19. 'cover_url',
  20. 'goods_category_ids',
  21. ];
  22. protected function boot(): void
  23. {
  24. parent::boot();
  25. static::addGlobalScope('status', function (Builder $builder) {
  26. $builder->where(['status' => 1])->orderBy('sort', 'desc');
  27. });
  28. }
  29. public function getGoodsTypesAttribute()
  30. {
  31. return self::query()->where(['parent_id' => $this->attributes['id']])->orderBy('sort', 'desc')->get()->toArray();
  32. }
  33. public function getGoodsCategoryIdsAttribute()
  34. {
  35. return GoodsCategory::query()->where(['category_id' => $this->attributes['id']])->orderBy('sort', 'desc')->pluck('id');
  36. }
  37. public function getCoverUrlAttribute()
  38. {
  39. return $this->attachmentService->switchImgToAliOss($this->attributes['cover_img']);
  40. }
  41. }