'integer', 'supplier_id' => 'integer', 'shelf_life' => 'integer', 'stock' => 'integer', 'sort' => 'integer', 'status' => 'integer', 'cost_price' => 'decimal:2', 'created_at' => 'datetime:Y-m-d H:i:s', 'updated_at' => 'datetime:Y-m-d H:i:s', ]; protected $appends = ['images_arr']; /** * 成本价属商业敏感数据,默认不随 toArray 输出(防止泄漏到小程序/供应商端); * 后台管理接口需在查询结果上调用 makeVisible('cost_price') 恢复。 */ protected $hidden = ['cost_price']; /** * 封面图片 */ public function imageIds(): Attribute { return Attribute::make( get: fn ($value) => explode(',', $value), set: fn ($value) => is_array($value) ? implode(',', $value) : $value, ); } // 获取封面图片关联数据 public function getImagesArrAttribute(): array { if( $this->image_ids ) { return SysFileModel::whereIn('id', $this->image_ids )->get()->toArray(); } return []; } /** * 所属分类 */ public function category(): BelongsTo { return $this->belongsTo(ProductCategoryModel::class, 'category_id', 'id'); } /** * 供货供应商 */ public function supplier(): BelongsTo { return $this->belongsTo(SupplierModel::class, 'supplier_id', 'id'); } /** * 多等级价格(同一商品按客户等级定价) */ public function prices(): HasMany { return $this->hasMany(ProductPriceModel::class, 'product_id', 'id'); } }