Hello i have somes problem with collection. I want to increment a value in a row of my collection but i can't figure it. Seriously collection make me headache right now.
i want the equivalent of :
$itemcollection
->where('item_name', $itemcollected_row->first()->item_name)
->increment('quantity',1);
in that code:
//verify is the item is in the collection
$itemcollection = collect();
$itemverification = $itemcollection->where('item_name',$itemcollected_row->first()->item_name);
if ($itemverification->count() > 0) { //if the item exist in collection update it
$itemcollection->where('item_name',$itemcollected_row->first()->item_name)->increment('quantity',1);
}else { //else we need to add it to the collection
$itemcollection->push([
'item_name' => $itemcollected_row->first()->item_name,
'item_avatar' => $itemcollected_row->first()->item_avatar,
'quantity' => 1,
'badge' => '<span class="badge badge-soft-secondary">Common</span>',
]);
}
I found a way to do it. This is now working
$itemcollection = collect();
//Push the item in the collection
$itemcollection->push([
'item_id' => $itemcollected_row->first()->item_id,
'item_name' => $itemcollected_row->first()->item_name,
'item_avatar' => $itemcollected_row->first()->item_avatar,
'quantity' => 1,
'badge' => '<span class="badge badge-soft-secondary">Common</span>',
]);
//loop to display all items collected
$display = '';
foreach ($itemcollection as $itemcollected) {
$display = $itemcollected['item_name']. ' ';
$displaycount = $itemcollection->where('item_name',$itemcollected['item_name'])->count();
$display .= $displaycount . '---';
$itemcollected .= $display;
}