I use filter
method from Collection
class to remove some objects from collection. But after that operation, sometimes objects with keys eg. 1, 4, 5 left. I would like to always have elements with order 0, 1, 2, 3 etc. after filter
action.
Is there any elegant way to do it without rewriting table to a new one?
Thanks!
You can use Laravel Collection's values()
method to make the the keys of a collection in a serialized order like this:
// Just for demonstration $collection = collect([ 10 => ['fruit' => 'Apple', 'price' => 200], 11 => ['fruit' => 'Mango', 'price' => 500] ]); $values = $collection->values(); $values->all(); /* Result would be: [ 0 => ['fruit' => 'Apple', 'price' => 200], 1 => ['fruit' => 'Mango', 'price' => 500], ] */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With