【问题标题】:How to subtract my product quantity after order is placed - laravel 5.2下订单后如何减去我的产品数量 - laravel 5.2
【发布时间】:2016-08-16 11:51:49
【问题描述】:

我有一个产品表,用于跟踪产品数量。我在前端的用户可以选择他们想要添加到购物车的数量,然后购买。一旦他们购买了产品,我想从该产品的产品表数量中减去他们购买的数量。

EX) 所以他们买了 2 部 iPhone,而 Products 表中有 5 部 iPhone,我想在插入订单后在 Products 表中拥有 3 部 iPhone。

这是我的 Products 表(注意“product_qty”字段)

我的 order_product 表(注意为该产品购买的“数量”)

还有我的 postOrder 函数:

public function postOrder(Request $request) {

        // Validate each form field
        $validator = Validator::make($request->all(), [
            'first_name' => 'required|max:30|min:2',
            'last_name'  => 'required|max:30|min:2',
            'address'    => 'required|max:50|min:4',
            'address_2'  => 'max:50|min:4',
            'city'       => 'required|max:50|min:3',
            'state'      => 'required|',
            'zip'        => 'required|max:11|min:4',
            'full_name'  => 'required|max:30|min:2',
        ]);


        // If error occurs, display it
        if ($validator->fails()) {
            return redirect('/checkout')
                ->withErrors($validator)
                ->withInput();
        }


        // Set Inputs to the the form fields so we can store them in DB
        $first_name = Input::get('first_name');
        $last_name = Input::get('last_name');
        $address = Input::get('address');
        $address_2 = Input::get('address_2');
        $city = Input::get('city');
        $state = Input::get('state');
        $zip = Input::get('zip');
        $full_name = Input::get('full_name');

        // Set $user_id to the currently authenticated user
        $user_id = Auth::user()->id;

        // Set $cart_products to the Cart Model with its products where
        // the user_id = to the current signed in user ID
        $cart_products = Cart::with('products')->where('user_id', '=', $user_id)->get();

        // Set $cart_total to the Cart Model alond with all its products, and
        // where the user_id = the current signed in user ID, and
        // also get the sum of the total field.
        $cart_total = Cart::with('products')->where('user_id', '=', $user_id)->sum('total');

        //  Get the total, and set the charge amount
        $charge_amount = number_format($cart_total, 2) * 100;

        // Create the order in DB, and assign each variable to the correct form fields
        $order = Order::create (
            array(
                'user_id'    => $user_id,
                'first_name' => $first_name,
                'last_name'  => $last_name,
                'address'    => $address,
                'address_2'  => $address_2,
                'city'       => $city,
                'state'      => $state,
                'zip'        => $zip,
                'total'      => $cart_total,
                'full_name'  => $full_name,
            ));

        // Attach all cart items to the pivot table with their fields
        foreach ($cart_products as $order_products) {
            $order->orderItems()->attach($order_products->product_id, array(
                'qty'    => $order_products->qty,
                'price'  => $order_products->products->price,
                'reduced_price'  => $order_products->products->reduced_price,
                'total'  => $order_products->products->price * $order_products->qty,
                'total_reduced'  => $order_products->products->reduced_price * $order_products->qty,
            ));
        }


        // Insert Quantity count here????


        // Delete all the items in the cart after transaction successful
        Cart::where('user_id', '=', $user_id)->delete();

        // Then return redirect back with success message
        flash()->success('Success', 'Your order was processed successfully.');

        return redirect()->route('cart');

    }

如何获取我刚购买的产品数量,并从“产品”表的数量字段中减去它?

【问题讨论】:

  • laravel.com/docs/5.1/queries#updates。您可以使用查询更新,例如 DB::table('products')->decrement('product_qty', $order_products->qty);
  • 您的上述说法有效。谢谢

标签: php laravel


【解决方案1】:

正如金枪鱼所说的 tuna 在 cmets 中所说的。

DB::table('products')->decrement('product_qty', $order_products->qty); 

【讨论】:

    猜你喜欢
    • 2020-09-05
    • 2017-01-28
    • 1970-01-01
    • 2019-04-21
    • 2017-11-13
    • 2021-04-12
    • 1970-01-01
    • 2020-12-16
    • 2014-05-04
    相关资源
    最近更新 更多