Forget doesn't work

Viewed 8448

If I try to remove an item from this collection

$examples = Example::where('example', '=', $data['example'])->get();

by doing

$examples->forget(20);

it doesn't remove the item from the collection, I still get back all the items that were in there originally. I have read the Laravel documentation and the api docs. And it should work (I think) but it doesn't.

Could someone point me out what I am doing wrong here?

This still returns an object.

$examples->forget(49);
return $examples->find(49);

P.S. Ever other method like push or get works.

Thanks alot!

4 Answers

you could do

$examples->reject(20);

Like most other collection methods, reject returns a new collection instance; it does not modify the collection it is called on.

Related