0
我有一個產品表,我在那裏跟蹤產品數量。我在前端的用戶可以選擇他們想要添加到購物車的數量,然後購買。一旦他們購買產品,我想從該產品的產品表數量中減去他們購買的數量。如何在訂購後減去我的產品數量 - laravel 5.2
EX)所以他們買了2個iPhone,在Products表中有5個iPhone,我想在插入訂單後在Products表中有3個iPhone。
我order_product表(注意是「數量」這是買了該產品)
而我的後序功能:
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');
}
如何獲得我剛購買的產品數量,然後從「產品」表格數量字段中減去它?
https://laravel.com/docs/5.1/queries#updates。你可以使用查詢更新例如DB :: table('products') - >遞減('product_qty',$ order_products-> qty); – tuna
我會檢查它 – David
您的聲明以上工作。謝謝 – David