使用Laravel下订单后,减少数据库中的产品数量

时间:2016-09-20 11:50:44

标签: php mysql laravel-4 cart

我在网站上有购物车,到目前为止一切都很完美。现在,我正在尝试为管理员可以在后端(已经完成)添加的每个产品以及客户订购产品以减少数据库中的数量。

据我所说,管理面板已准备就绪,可以将数量添加到保存在数据库中的产品中。这是我的购物车提交控制器

public function orderSubmit() {
    $cart = Session::get(self::CART_SESSION_KEY, array());
    if (count($cart) < 1) {
        return Redirect::to('/');
    }

    $validatorRules = array(
        'captcha' => 'required|captcha'      
    );

    Input::merge(array_map('trim', Input::all()));
    $validator = Validator::make(Input::all(), $validatorRules);

    if ($validator->fails()) {
        return Redirect::to('/cart/order?_token=' . csrf_token())->withErrors($validator->errors())->withInput(Input::except(['captcha']));
    }

    $order = new Order();
    $order->user_id = self::$user->user_id;
    $order->data = json_encode($cart, true);
    $order->info = Input::get('additional_info');

    $order->save();
    Session::put(self::CART_SESSION_KEY, array());

    return Redirect::to('/)->with('message_success', 'Order created! We will contact you shortly to confirm your order details.');
}

购物车保存在$order->data = json_encode($cart, true);中,并保存在产品的数据库信息等中

{"title":"Test Product","description":"You save 25%","quantity":1,"price":135}

如果上面有多个产品,那么数组也会有它们。

我能理解的是如何提取订购的产品以及产品的数量,以便我以后可以更新每种产品的数据库数量字段?

我甚至不知道从哪里开始。

2 个答案:

答案 0 :(得分:3)

假设$cart是一个数组且基于您提供的json,您需要:

  1. 将产品ID添加到$cart,否则几乎不可能知道该订单的产品是什么
  2. 使用$cart上每个产品的产品ID,您现在可以循环购物车产品并进行更新。类似下面的代码。

    public function orderSubmit() {
        $cart = Session::get(self::CART_SESSION_KEY, array());
        if (count($cart) < 1) {
            return Redirect::to('/');
        }
    
        $validatorRules = array(
            'captcha' => 'required|captcha'      
        );
    
        Input::merge(array_map('trim', Input::all()));
        $validator = Validator::make(Input::all(), $validatorRules);
    
        if ($validator->fails()) {
            return Redirect::to('/cart/order?_token=' . csrf_token())->withErrors($validator->errors())->withInput(Input::except(['captcha']));
        }
        \DB::beginTransaction();
    
        try {
            $order = new Order();
            $order->user_id = self::$user->user_id;
            $order->data = json_encode($cart, true);
            $order->info = Input::get('additional_info');
    
            $order->save();
    
            foreach ($cart as $item) {
                $product = Product::find($item->id);
                $product->decrement('votes', $item->quantity);
            }
    
            Session::put(self::CART_SESSION_KEY, array());
    
            \DB::commit();
    
            return Redirect::to('/')->with('message_success', 'Order created! We will contact you shortly to confirm your order details.');
        } catch (\Exception $ex) {
            \DB::rollback();
    
            return Redirect::to('/cart/order?_token=' . csrf_token())->withErrors($ex->getMessage())->withInput(Input::except(['captcha']));
        }
    }
    
  3. 我添加了交易代码,以确保在订单正确存储时数量减少。

答案 1 :(得分:0)

对于使用资源控制器更简单/简单答案的用户:

public function create()
{
    $stocks = Stock::all();
    //dd($stocks);
    return view('sales.create', compact('stocks'));
    //$sales = Sale::pluck('stock_id')->prepend('stock_id');
    //$sales = DB::table('stocks')->select('stock_id')->get();
    //return view('sales.create')->with('sales',$sales);
}


public function store(Request $request)
{
    $this->validate($request, [
        'stock_id' => 'required',
        'product_name' => 'required',
        'sale_quantity' => 'required',
        'unit_selling_price' => 'required',
        'total_sales_cost' => 'required'
        ]);

    //create stock
    $sale = new Sale;
    $sale->stock_id = $request->input('stock_id');
    $sale->product_name = $request->input('product_name');
    $sale->sale_quantity = $request->input('sale_quantity');
    $sale->unit_selling_price = $request->input('unit_selling_price');
    $sale->total_sales_cost = $request->input('total_sales_cost');
    $sale->stock_quantity =
        $sale->save();
    DB::table('stocks')->where('stock_name', $request->input('product_name'))->decrement('stock_quantity', $request->input('sale_quantity'));

    return redirect('/sales')->with('success', 'Sale Saved');
}