Laravel: How to use foreach loop to decrease stock based on order quantity - Stack Overflow

PHOTO EMBED

Fri May 20 2022 14:05:23 GMT+0000 (Coordinated Universal Time)

Saved by @WMOH #php

public function store(Request $request)
{
    // ...

    // iterate (loop) over each item in the order items collection
    $order->items->each(function ($item) {
        // deduct the quantity of the item for this order
        // from the stock of the product and update the new stock
        $item->update(['stock' => ($item->stock - $item->pivot->quantity)]);
    });

    // ...
}
content_copyCOPY

https://stackoverflow.com/questions/67497441/laravel-how-to-use-foreach-loop-to-decrease-stock-based-on-order-quantity