请就我的代码如何正常工作给我一些建议。我有一个循环,它给了我一个 id 和数量,以将每个循环更新到我的数据库中。
但是当我将它传递给我的私有函数并尝试执行它时出现错误。我只是 PHP/Laravel 的初学者。这是我的完整代码:
public function updateProduct(Request $request) {
$ids = array();
foreach ($request->products as $ship_id) {
$product_id = DB::table('shipping_products')->select('product_id','quantity')
->where(['shipping_products.shipping_id'=>$ship_id])
->get();
array_push($ids,$product_id);
}
foreach ($ids as $value) {
foreach ($value as $update) {
$this->updateProductQty($update->product_id,$update->quantity);
}
}
}
private function updateProductQty($product_id, $quantity_order) {
$qty = DB::table('products')->select('quantity')
->where(['products.product_id'=>$product_id])
->get();
$updateTotalQty = $qty - $quantity_order;
DB::table('products')
->where('product_id', $product_id)
->update(array(
'quantity' => $updateTotalQty
));
}
我在这行有一个错误:
$this->updateProductQty($update->product_id,$update->quantity);
它说错误信息:
Object of class Illuminate\Support\Collection could not be converted to int