Laravel雄辩的关系一对多

问题描述 投票:0回答:1

我正在处理三个表:产品,交货和库存。我希望当产品交付时,库存表中的数量增加。因此,在交付表的控制器中,我编写了以下代码:

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

但是当我交货时,库存表中的数量不变。

laravel eloquent one-to-many
1个回答
0
投票

PHP中的简单类型不是通过引用获得的,因此必须将新值分配给Stock对象。这是假设quantiteStock模型上是一个整数,由于您是在Quantite上调用->save()的,因此在您提供的代码中并不能完全清楚。记住要保存Stock模型而不是quantite

$stock = $produit->stock;
$stock->quantite = $stock->quantite + $delivery['quantite']
$stock->save();
© www.soinside.com 2019 - 2024. All rights reserved.