Laravel-Collection没有增量方法



你好,我在收集方面有一些问题。我想在我收藏的一行中增加一个值,但我想不出来。严重的收藏现在让我头疼。

我想要相当于的

$itemcollection
->where('item_name', $itemcollected_row->first()->item_name)
->increment('quantity',1);

在该代码中:

//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>',
]);
}

我找到了一种方法

$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;
}

最新更新