Overview
TheListCollection constructor creates a new list collection from an array, iterable, or Arrayable object. It automatically reindexes all keys to sequential integers starting from 0, ensuring the list structure is maintained.
Signature
items
\Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue>|null
default:"[]"
The items to include in the collection. Can be an array, Collection, or any iterable object.
Behavior
The constructor performs two key operations:- Calls parent constructor: Initializes the Collection with the provided items
- Reindexes keys: Converts all keys to sequential integers (0, 1, 2, …) using
array_values()
Examples
Creating from associative array
Associative keys are discarded and replaced with sequential integers:Creating from non-sequential keys
Non-sequential integer keys are reindexed:Creating from empty input
Creating from a Collection
You can create a ListCollection from an existing Laravel Collection:Creating with single item
Key Points
- All keys are automatically converted to sequential integers (0, 1, 2, …)
- Original keys are discarded, preserving only values and their order
- Works with arrays, Collections, and any iterable objects
- Empty collections are valid and contain no elements