Laravel - Нет метода увеличения для Коллекции - PullRequest
0 голосов
/ 19 марта 2020

Здравствуйте, у меня есть проблемы с коллекцией. Я хочу увеличить значение в строке моей коллекции, но не могу понять это. Серьезно, коллекция вызывает у меня головную боль прямо сейчас.

Я хочу эквивалент:

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

1 Ответ

0 голосов
/ 20 марта 2020

Я нашел способ сделать это. Это сейчас работает

$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;
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...