我的数据透视表中有4个额外的属性(“product_id”,“quantity”,“discount_percent”,“discount_amount”),但是当我存储这些属性时,这些属性的值始终为0,其余属性则正确填充。有任何想法吗?

发票模型

public function productversion()
{
    return $this->belongsToMany('App\Productversion')->withPivot('product_id', 'quantity', 'discount_percent', 'discount_amount')->withTimestamps();
}

产品版本模型
public function invoice()
{
    return $this->belongsToMany('App\Invoice')->withPivot('product_id', 'quantity', 'discount_percent', 'discount_amount')->withTimestamps();
}

控制器(商店)
$invoice->productversion()->attach($productversionid, ['product_id' => $productid], ['quantity' => $qty], ['discount_percent' => $discountprc], ['discount_amount' => $discountamt]);

最佳答案

只需一个数组即可:

$invoice->productversion()->attach($productversionid, [
  'product_id' => $productid,
  'quantity' => $qty,
  'discount_percent' => $discountprc,
  'discount_amount' => $discountamt
]);

08-26 08:31