Skip to content
Advertisement

Updating product table quantity from different record

I am having an issue updating quantity from the main stock with different item quantity that has the different id.

This is the product table with the main stock location

I would like to subtract some quantity but it comes difficult to update all records with the same item.

Example: I have qty: 42, i want to subtract from the item with id : 3 how would I implemented in laravel php or the best way to do it ?

Advertisement

Answer

Try this : (not tested)

        $items = Product::whereItemId(3)->where('quantity','>', 0 )->get();
        $qty = 42;       
        foreach ($items as $item) {
            if($qty >= 0)     
            {       
                $substruct = $item->quantity < $qty ? $item->quantity: $qty;
                $qty = $qty - $substruct;                
                $item->update(['quantity' => $substruct]);
            }
        }
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement