Laravel 5.4:遍历数组并从每个对象中减去数量,直到数量达到 0 停止

Laravel 5.4: loop through array and minus quantity from each object until quantity reach 0 stops

我需要遍历数组并从每个对象中减去 needed_quantity

例如。 needed_quantity = 22

并且这个数组中有三个对象,每个对象数量=10

所以它会是这样的。

  1. 从expire_date取10件=2019-03-02库存将变为0
  2. 从expire_date取10件=2019-03-10库存将变为0
  3. 从expire_date取2件=2019-03-11库存将变为8件

这是数组

foreach ( $orderProducts as $order_product ) {
                    //This the quantity in order details.
                    $orderQuantity = $order_product->quantity;
    $currentInventors = Inventory::whereProductId( $order_product->product_id )->get()->sortBy( 'expire_date' );

    if ( $currentInventors != null ) {
        foreach ( $currentInventors as $currentInventory ) {
            $takeQuantity = $currentInventory->inventory - $orderQuantity; //10-15 = 5
            if ( $currentInventory->inventory >= $orderQuantity ) {
                Inventory::whereId( $currentInventory->id )->update( [ 'inventory' => $takeQuantity ] );
                break;
            } elseif ( $currentInventory->inventory < $orderQuantity ) {
                Inventory::whereId( $currentInventory->id )->update( [ 'inventory' => 0 ] );
                //$takeQuantity = $currentInventory->inventory - $orderQuantity; //10-15 = -5
                $newQuantity = $orderQuantity - $currentInventory->inventory; //15-10 = 5
                if ( $currentInventory->inventory >= $orderQuantity ) {
                    Inventory::whereId( $currentInventory->id )->update( [ 'inventory' => $newQuantity ] );
                    break;
                }
            }
        }
    }
}

现在,当我更新时,我得到了所有三个对象值 0

更新数组

Collection {#715
  #items: array:3 [
    0 => Inventory {#744
      #fillable: array:3 [
        0 => "product_id"
        1 => "inventory"
        2 => "expire_date"
      ]
      #attributes: array:6 [
        "id" => 10
        "product_id" => 857
        "inventory" => 10
        "expire_date" => "2019-03-02"
        "created_at" => "2019-03-07 11:13:21"
        "updated_at" => "2019-03-27 11:16:01"
      ]
    }
    1 => Inventory {#745
      #fillable: array:3 [
        0 => "product_id"
        1 => "inventory"
        2 => "expire_date"
      ]
      #attributes: array:6 [
        "id" => 13
        "product_id" => 857
        "inventory" => 10
        "expire_date" => "2019-03-10"
        "created_at" => "2019-03-10 16:53:21"
        "updated_at" => "2019-03-27 11:16:01"
      ]
    }
    2 => Inventory {#746
      #fillable: array:3 [
        0 => "product_id"
        1 => "inventory"
        2 => "expire_date"
      ]
      #attributes: array:6 [
        "id" => 16
        "product_id" => 857
        "inventory" => 0
        "expire_date" => "2019-03-11"
        "created_at" => "2019-03-10 17:01:14"
        "updated_at" => "2019-03-27 11:16:01"
      ]
    }
  ]
}

这是我喜欢的解决方案。 我的错是我没有在循环中每次都减少 $orderQuantity

$orderProducts = OrderDetail::whereOrderListsId( $deliveryList->order_lists_id )->get();

foreach ( $orderProducts as $order_product ) {
    //This the quantity in order details.
    $orderQuantity = $order_product->quantity;
    //Here we selected the oldest expire date of the same product in the inventory.
    $currentInventors = Inventory::whereProductId( $order_product->product_id )->get()->sortBy( 'expire_date' );
    if ( $currentInventors != null ) {
        foreach ( $currentInventors as $currentInventory ) {
            $takeQuantity = $currentInventory->inventory - $orderQuantity;
            if ( $currentInventory->inventory >= $orderQuantity ) {
                Inventory::whereId( $currentInventory->id )->update( [ 'inventory' => $takeQuantity ] );
                break;
            } else {
                $orderQuantity = $orderQuantity - $currentInventory->inventory;
                Inventory::whereId( $currentInventory->id )->update( [ 'inventory' => 0 ] );
            }
        }
    }
}

现在工作正常。