Stratos
163
2
Laravel

Help with collections.

Posted 2 years ago by Stratos

Consider the following collection:

$cart = collect([
    ['code' => '123', 'name' => 'Item 1',  'quantity' => 1],
    ['code' => '456', 'name' => 'Item 1',  'quantity' => 1]
]);

I want it so that if I were to push a new item with the same code like 123 I want to increase quantity by 1.

Right now push method just appends the new item at the end. How do I achieve this? Thanks.

Please sign in or create an account to participate in this conversation.