'decimal:2', 'unit_price' => 'decimal:2', 'subtotal' => 'decimal:2', 'received_quantity' => 'decimal:2', ]; // 移除 $appends 以避免自動附加導致的錯誤 // 這些屬性將在 Controller 中需要時手動附加 // protected $appends = ['productName', 'unit']; public function getProductNameAttribute(): string { return $this->product?->name ?? ''; } public function getUnitAttribute(): string { // 優先使用採購單位 > 大單位 > 基本單位 // 與 PurchaseOrderController 的邏輯保持一致 if (!$this->product) { return ''; } return $this->product->purchase_unit ?: ($this->product->large_unit ?: $this->product->base_unit); } public function purchaseOrder(): BelongsTo { return $this->belongsTo(PurchaseOrder::class); } public function product(): BelongsTo { return $this->belongsTo(Product::class); } }