【问题标题】:Reduce stock number in laravel减少laravel中的库存数量
【发布时间】:2018-07-01 18:25:44
【问题描述】:

如何减少laravel的库存数量?

在我的产品表中,我有 stock 列,我在其中设置了我拥有的商品数量,我想在 2 种情况下减少它:

1- 何时添加到订单表(而不是产品在用户购物车中)

2-当订单状态未取消时(如果订单状态变为已取消库存增加回来)

PS:目前我没有这件事的代码,所以我没有分享 任何代码,我正在寻找有关如何完成它的想法。基于此,我会 制作函数并在这里分享完成。

订单的JSON

"[{\"id\":29,\"name\":\"effewf\",\"price\":24524,\"quantity\‌​":1,\"attributes\":[‌​{\"attr\":{\"label\"‌​:\"Gray\",\"price\":‌​\"7000.00\"}}],\"con‌​ditions\":[]},{\"id\‌​":27,\"name\":\"new product\",\"price\":7246,\"quantity\":2,\"attributes\":[],\"‌​conditions\":[]}]"

更新

基于@btl 的回答,我在order 模型中添加了以下代码:

protected static function boot()
  {
      parent::boot();


      $productIdsAndQuantities = $this->products->map(function ($product) {
        return [$product->id => $product->quantity];
      });

      static::updating(function (Order $order) {
          // restock if cancelled
          $oldStatus = OrderStatus::find($order->getOriginal('orderstatus_id'));
          if ($oldStatus->title !== 'Cancelled' && $order->orderstatus->title === 'Cancelled') {
               $order->products->each(function(Product $product) {
                    $stock = $product->getAttribute('stock');
                    $product->update([
                      'stock' => $stock + $product->order->getAttribute($productIdsAndQuantities)
                    ]);
               });
          }
      });
  }

我认为这段代码需要在$productIdsAndQuantities$product->update(['stock' => $stock + $product->order->getAttribute($productIdsAndQuantities)]); 部分修复才能使用它。

目前,当我尝试将购物车详细信息添加到订单表时,出现以下错误

不在对象上下文中使用 $this

更新 2

我将代码更改为:

protected static function boot()
  {
      parent::boot();



      static::updating(function (Order $order) {
        $productIdsAndQuantities = $order->product_name->map(function ($product) {
          return [$product->id => $product->quantity];
        });
          // restock if cancelled
          $oldStatus = OrderStatus::find($order->getOriginal('orderstatus_id'));
          if ($oldStatus->title !== 'Cancelled' && $order->orderstatus->title === 'Cancelled') {
               $order->products->each(function(Product $product) {
                    $stock = $product->getAttribute('stock');
                    $product->update([
                      'stock' => $stock + $product->order->getAttribute($productIdsAndQuantities)
                    ]);
               });
          }
      });
  }

现在我的订单将被下达,但库存列没有任何变化。

有什么想法吗?

【问题讨论】:

  • 您应该创建另一个具有关联 order_id 和 product_id 的表名 stock 然后添加股票上涨或下跌的详细信息。
  • @SagarGautam 那么我的产品表中的库存列呢?
  • 股票列有初始值吗?
  • 由于只是增加/减少库存值可能会在以后引起问题,并且您必须跟踪库存值列的整体变化,我认为您需要另一个表。
  • 这是因为你还没有开始编码LOL,我会尝试添加一些代码。

标签: laravel


【解决方案1】:

已解决

诀窍是在我的CartController 上使用try {,当我想将我的数据保存在orders 表中时,我应该将我的代码放在:

try {
        $order = new Order();
        $qty = Cart::getTotalQuantity();
        $order->product_data = $cartItems;
        $order->user_id = Auth::user()->id;
       // rest of it...
        Auth::user()->orders()->save($order);
        //trick is in this part
        foreach ($cartItems as $item) {
            $product = Product::find($item->id);
            $product->decrement('stock', $item->quantity);
        }

答案已提供here

【讨论】:

    【解决方案2】:

    注册模型事件以处理订单更新。

    在您的订单模型中:

    protected static function boot()
    {
        parent::boot();
    
        static::updating(function (Order $order) {
            // restock if cancelled
            $oldStatus = OrderStatus::find($order->getOriginal('orderstatus_id');
            if ($oldStatus->name !== 'cancelled' && $order->orderstatus->name === 'cancelled') {
                 $order->products->each(function(Product $product) {
                      $stock = $product->getAttribute('stock');
                      $product->update(['stock' => $stock + $product->order->getAttribute('quantity_of_product_ordered')]);
                 });
            }
    
            // added to order table similar to above...
            // check if it's a new product id being added to the order, decrease stock quantity accordingly...
            // ...
        });
    }
    

    【讨论】:

    • 所以按照你的方式我不需要@SagarGautam 建议的第三张桌子?
    • 另一件事(that's my bad i didn't mention in question) 我的订单状态是动态的,我通过id 在名为orderstatus_id 的列中获取它们,我应该更改代码中的任何内容吗?
    • 我认为没有必要使用stocks 表,除非您正在跟踪产品移动并需要它的历史记录。在这种情况下,您的订单状态只会添加另一个查询来查找正确的状态 ID。与我现在的字符串相比,这不会是直接的 === 比较。
    • 我更新了答案,但猜到了一些属性和关系名称,您应该相应地更改它们。
    • 好的,这是订单模型,我也应该将它添加到产品模型中吗?这是从哪里来的? quantity_of_product_ordered
    【解决方案3】:

    你的桌子看起来像,

    产品

        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
    
            ......
    
            $table->integer('stock');
            $table->timestamps();
    
        });
    

    库存

        Schema::create('stocks', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('product_id')->unsigned();
            $table->integer('order_id')->unsigned();
            $table->enum('fall_rise',[-1,1]);
            $table->timestamps();
    
            $table->foreign('product_id')->references('id')->on('products');
            $table->foreign('order_id')->references('id')->on('orders');
        });
    

    订单

        Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
    
            .....
    
            .....
    
            $table->timestamps();
        });
    

    现在,添加一个库存价值为 10 的示例产品

     $product = new Product();
     $product->name = "abc";
    
     ......
    
     $product->stock = 10;
     $product->save();
    

    现在,订购 abc 时,在订单表中添加详细信息。现在,要减少库存值,然后将产品中库存列的值更新 10-1 = 9,在库存表中添加记录,如

    样本值,

     product_id = 1;
     order_id = 1;
    
     ....
    
     fall_rise = -1;
    

    这样,当订购了 10 个库存产品时,库存列变为零,因此产品不可用。如果您只想获取可用产品,请使用类似查询,

    $available_products = Product::where('stock', '>', 0)->get();
    

    希望你能理解。

    【讨论】:

    • @mafortis 好的,我希望它会有所帮助
    • 在我运行迁移命令之前有 1 个问题?您在架构中提到了$table->enum('fall_rise',[-1,1]);,如果用户订购了超过 1 件的某些产品怎么办?仍然从库存中减 1?
    • 我想买一只股票,你可以做整数列fall_rise,这样你就可以做任何股票价值的变化。
    猜你喜欢
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 2014-05-04
    • 2017-01-28
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    相关资源
    最近更新 更多