belongsTo(Product::class); } public function coupon() { return $this->hasMany(Coupon::class); } public function fav() { return $this->hasOne(UserFav::class); } public function agent() { return $this->belongsTo(Agent::class); } public function category() { return $this->belongsTo(Category::class); } public function user() { return $this->hasOne(User::class, 'id', 'verifier'); } public function guide() { return $this->belongsTo(Guide::class); } public function item() { return $this->hasMany(AgentProductItem::class); } public function setChannelIdAttribute($value) { if (is_array($value)) { $this->attributes['channel_id'] = join(',', array_filter($value)); } } public function setPicturesAttribute($value) { if (is_array($value)) { $this->attributes['pictures'] = json_encode(array_filter($value)); } } public function setProductIdsAttribute($value) { if (is_array($value)) { $this->attributes['product_ids'] = join(',', array_filter($value)); } } // 获取所有产品图片 public function getPicturesAttribute($value): array { if (is_string($value)) { $value = $value ? json_decode($value, true) : []; } $this->append('picture'); return $value ?? []; } // 获取第一张产品图片 public function getPictureAttribute($value): string { return $this->pictures[0] ?? ''; } //列表查询统一查询条件 public function scopeList($query) { return $query->whereHas('product', function ($query) { return $query->where('status', ProductStatus::ON_SALE)->where('stock', '>', 0); }) ->where('status', ProductStatus::ON_SALE)->where('stock', '>', 0) ->select('id', 'sale', 'product_id', 'price', 'original_price', 'title', 'pictures'); } }