0
商品数量を把握するための商品表があります。フロントエンドのユーザーは、買い物カゴに追加する数量を選択して購入することができます。彼らが商品を購入すると、その商品の商品表数量から、購入した数量を差し引いてみたいと思います。ご注文後に商品数を減算する方法 - laravel 5.2
EX)2台のiPhoneを購入し、5台のiPhoneがProductsテーブルにあるので、注文が挿入された後にProductsテーブルに3台のiPhoneがあります。ここで
は私のProductsテーブル(「product_qty」フィールドに注意してください)![Products Table](https://i.stack.imgur.com/PmQtg.jpg)
(その製品の購入された「数量」を注意してください)そして、私の後順の関数です。
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') - > decrement( 'product_qty'、$ order_products-> qty)などのクエリ更新を使用できます。 – tuna
私はそれを確認します – David
あなたの上記のステートメントが働いた。ありがとう – David