Laravel - Collection 没有递增方法
Laravel - No increment method for Collection
你好,我在使用 collection 时遇到了一些问题。我想在 collection 的一行中增加一个值,但我无法理解。说真的 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;
}
你好,我在使用 collection 时遇到了一些问题。我想在 collection 的一行中增加一个值,但我无法理解。说真的 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;
}