簡體   English   中英

Laravel eloquent 關系一對多

[英]Laravel eloquent relationship one to many

我正在處理三張表:產品、交貨和庫存。 我希望在交付產品時,它在庫存表中的數量會增加。 所以在交貨表的controller中我寫了這段代碼:

$produit = Produit::find($delivery['produit_id']); 
$quantite = $produit->stock->quantite;
$quantite += $delivery['quantite'];
$quantite->save();

但是當我發貨時,庫存表中的數量不會改變。

只需鍵入PHP不是通過引用,因此您必須將新值分配給Stock object。 這是假設quantiteStock model 上的 integer,這在您提供的代碼中並不完全清楚,因為您在 quantite 上調用->save() 請記住保存Stock model 而不是quantite

$stock = $produit->stock;
$stock->quantite = $stock->quantite + $delivery['quantite']
$stock->save();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM