Laravel-无增量收集方法

Laravel-无增量收集方法,laravel,laravel-collection,Laravel,Laravel Collection,你好,我在收集方面有些问题。我想在集合的一行中增加一个值,但我无法计算它。我现在真的很头疼 我想要相当于: $itemcollection ->where('item_name', $itemcollected_row->first()->item_name) ->increment('quantity',1); 在该法典中: //verify is the item is in the 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();
$itemverification=$itemcollection->where('item\u name',$itemcollected\u row->first()->item\u name);
如果($itemverification->count()>0){//如果集合中存在该项,则更新它
$itemcollection->其中('item_name',$itemcollected_row->first()->item_name)->增量('quantity',1);
}else{//否则我们需要将其添加到集合中
$itemcollection->push([
'item_name'=>$itemcollected_行->first()->item_名称,
'item\u avatar'=>$itemcollected\u row->first()->item\u avatar,
“数量”=>1,
“徽章”=>“普通”,
]);
}

我找到了一种方法。这是现在的工作

$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;
        }
$itemcollection=collect();
//推送集合中的项目
$itemcollection->push([
'item\u id'=>$itemcollected\u row->first()->item\u id,
'item_name'=>$itemcollected_行->first()->item_名称,
'item\u avatar'=>$itemcollected\u row->first()->item\u avatar,
“数量”=>1,
“徽章”=>“普通”,
]);
//循环以显示收集的所有项目
$display='';
foreach($itemcollection作为$itemcollected){
$display=$itemcollected['item_name'].';
$displaycount=$itemcollection->where('item_name',$itemcollected['item_name'])->count();
$display.=$displaycount'--';
$itemcollected.=$display;
}

对不起,我想要的是等价的:$itemcollection->where('item\u name',$itemcollected\u row->first()->item\u name)->increment('quantity',1);你需要更详细地解释。什么是
$itemcollected\u行
,以及为什么
collect()
为空,但您正在查找
item\u名称