微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel:下订单后更新股票价值

如何解决Laravel:下订单后更新股票价值

我想在下订单时更新库存价值。 挑战是产品在属性表中具有不同的属性

Product Attribute Table

Small,Medium,Large,Extra large 

我想在下订单时分别更新产品属性库存。例如,当用户下订单时,用户选择的产品如

product_id = 1 size Small quantity is 7

所以 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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。