订购后如何减去我的产品数量 - laravel 5.2

时间:2016-04-22 18:17:03

标签: php laravel

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

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

这是我的产品表(请注意&#34; product_qty&#34;字段) Products Table

我的order_product表(注意&#34;数量&#34;为该产品购买的) Product Orders Table

我的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');

    }

我如何获得刚购买的产品数量,并从&#34;产品&#34;中减去产品数量。表数量字段?

1 个答案:

答案 0 :(得分:1)

正如托纳所说,tuna在评论中说。

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