D
D
Dmitry2018-07-18 08:03:21
Laravel
Dmitry, 2018-07-18 08:03:21

How to remove an item from a laravel collection?

There is a selection from the table

Collection {#561 ▼
  #items: array:3 [▼
    0 => Test {#562 ▶}
    1 => Test {#563 ▶}
    2 => Test {#564 ▶}
  ]
}

I'm going to loop through this collection and if some element does not satisfy me, I need to remove it.
That is, at the output after the loop, I need to get it like this:
Collection {#561 ▼
  #items: array:3 [▼
    0 => Test {#562 ▶}
    2 => Test {#564 ▶}
  ]
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Camaro67, 2018-07-18
@liggth

We read the documentation , there are a lot of interesting things.

$collection = collect(['Я', 'не', 'читаю', 'документацию']);
$collection->forget(1);
$collection->dump(); // ['Я', 'читаю', 'документацию']

And you can also work with a collection as with a regular array, which means that unsetit will also work: And even this is not all, in general, as I already said: read the documentation. unset($collection[1]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question