rotaercz rotaercz - 10 months ago 48
PHP Question

How can I add an item into a Laravel Eloquent Collection by index?

I tried the following but it doesn't work.

$index = 2;
$collection->put($index, $item4);

For example if $collection looks like this:

$collection = [$item1, $item2, $item3];

I'd like to end up with:

$collection = [$item1, $item2, $item4, $item3];


The easiest way would probably be to splice it in, like this:

$collection = $collection->splice(2, 0, [$item4]);

Collections usually support the same functionality as regular PHP arrays. In this case, it's the array_splice() function that's used behind the scenes.

By setting the second parameter to 0, you essentially tell PHP to "go to index 2 in the array, then remove 0 elements, then insert this element I just provided you with".