问题描述
我想在下订单时更新库存价值。 挑战是产品在属性表中具有不同的属性。
Small,Medium,Large,Extra large
我想在下订单时分别更新产品属性库存。例如,当用户下订单时,用户选择的产品如
product_id = 1 size Small quantity is 7
结帐
// checkout
public function checkout(Request $request)
{
if ($request->isMethod('post')) {
$data = $request->all();
DB::beginTransaction();
// Get cart details (Items)
$cartItems = Cart::where('user_id',Auth::user()->id)->get()->toArray();
foreach ($cartItems as $key => $item) {
# code...
$cartItem = new OrdersProduct;
$cartItem->order_id = $order_id;
$cartItem->user_id = Auth::user()->id;
// Get products details
$getProductDetails = Product::select('product_code','product_name','product_color')->where('id',$item['product_id'])->first()->toArray();
$cartItem->product_id = $item['product_id'];
$cartItem->product_code = $getProductDetails['product_code'];
$cartItem->product_name = $getProductDetails['product_name'];
$cartItem->product_color = $getProductDetails['product_color'];
$cartItem->product_size = $item['size'];
$getdiscountedAttrPrice = Product::getdiscountedAttrPrice($item['product_id'],$item['size']);
$cartItem->product_price = $getdiscountedAttrPrice['final_price'];
$cartItem->product_qty = $item['quantity'];
$cartItem->save();
// Want to Update the Product Attribute Table Stock
$item = new ProductsAttribute;
$item->where('product_id','=',$item['product_id'],'size',$item['size'])->decrement('stock',$request->quantity);
}
// Insert Order Id in Session variable for Thanks page
Session::put('order_id',$order_id);
DB::commit();
}
}
invalidargumentexception
Non-numeric value passed to decrement method.
当我像 decrement('stock',7) 这样直接输入值时,它会显示并出错
Illuminate\Database\QueryException
sqlSTATE[42000]: Syntax error or access violation: 1064 You have an error in your sql Syntax; check the manual that corresponds to your MariaDB server version for the right Syntax to use near '`product_id` is null' at line 1 (sql: update `products_attributes` set `stock` = `stock` - 7,`products_attributes`.`updated_at` = 2021-05-01 17:18:30 where size `product_id` is null)
解决方法
只需更改更新查询并使其简单明了。其余代码没问题。
// Want to Update the Product Attribute Table Stock
$product_attribute = ProductsAttribute::where(['product_id' => $item['product_id'],'size' => $item['size']])->first();
if($product_attribute){
$stock = $product_attribute->stock - (int) $request->quantity;
$product_attribute->update(['stock' => $stock]);
}
,
你必须将它扭曲成一个数组或使用两个更好且易于理解的地方
ERROR: Cannot set priority of datanode process 16536
ERROR: Cannot set priority of namenode process 16556
或者像这样:
$item->where([
['product_id','=',$item['product_id']],['size',$item['size']],])->decrement('stock',(int) $request->quantity);