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

标签 php mysql laravel-4 cart

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

到目前为止,管理面板已准备就绪,可以向保存在数据库中的产品添加数量。这是我的购物车提交 Controller

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}

如果按顺序有多个产品,上面的数组也会有它们。

我无法理解的是如何提取订购的产品以及其中的数量,以便稍后更新每个产品的数据库数量字段?

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

最佳答案

假设 $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']));
        }
    }
    

我添加了交易代码,以确保在正确存储订单时减少数量。

关于php - 使用 Laravel 下订单后减少数据库中的产品数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39593304/

相关文章:

php - 保存上传的媒体内容

mysql - 从一列中提取子字符串并将其放入另一个 SQL

PHP - MYSQL - 如果用户名存在,检查特定用户名的密码

mysql - mysql 中的 varchar 和复合主键?

mysql - 如何获取单列值的 sum()

php - Laravel 捕获 whoops 应用级异常并使用唯一 ID 发布到日志#

php - Laravel head 内容出现在 body 标签中

php - 如何在 Eloquent ORM Laravel 上添加多个 where 子句?

php - PHP 统计今天和昨天注册的用户数

php - 在 XAMPP 的 php.ini 中增加 max_input_vars