2016-04-22 25 views
0

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

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

这里是我的产品表(注意“product_qty”字段) Products Table

我order_product表(注意是“数量”这是买了该产品) Product Orders Table

而我的后序功能:

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

    } 

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

+1

https://laravel.com/docs/5.1/queries#updates。你可以使用查询更新例如DB :: table('products') - >递减('product_qty',$ order_products-> qty); – tuna

+0

我会检查它 – David

+0

您的声明以上工作。谢谢 – David

回答

1

由于金枪鱼说tuna在评论中说。

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