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.
 
 

51 lines
1.2 KiB

<?php
namespace App\Model\v3;
use App\Model\Model;
use App\Service\v3\Interfaces\AttachmentServiceInterface;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\Di\Annotation\Inject;
class Category extends Model
{
use SoftDeletes;
/**
* @Inject
* @var AttachmentServiceInterface
*/
protected $attachmentService;
protected $table = 'lanzu_category';
protected $appends = [
'goods_types',
'cover_url',
'goods_category_ids',
];
protected function boot(): void
{
parent::boot();
static::addGlobalScope('status', function (Builder $builder) {
$builder->where(['status' => 1])->orderBy('sort', 'desc');
});
}
public function getGoodsTypesAttribute()
{
return self::query()->where(['parent_id' => $this->attributes['id']])->orderBy('sort', 'desc')->get()->toArray();
}
public function getGoodsCategoryIdsAttribute()
{
return GoodsCategory::query()->where(['category_id' => $this->attributes['id']])->orderBy('sort', 'desc')->pluck('id');
}
public function getCoverUrlAttribute()
{
return $this->attachmentService->switchImgToAliOss($this->attributes['cover_img']);
}
}