What is a collection?
Illuminate\Support\Collection is a wrapper around a PHP array that lets you chain operations instead of nesting array functions. Every method that transforms data returns a new collection, leaving the original untouched.
Collections are immutable. Each transformation method returns a new collection instance, so you can branch a pipeline without affecting earlier steps.
Creating collections
get() always returns an Illuminate\Database\Eloquent\Collection, which extends Collection and supports all the same methods.
range() — create a sequence of numbers
Returns a collection containing the integers in the given range.
Essential methods
map — transform each item
filter and reject — narrow the set
whereNull / whereNotNull — filter by null
whereNull keeps only the items whose given key is null, and whereNotNull does the opposite.
first and last — retrieve one item
pluck — extract one field
groupBy — organize by a key
sortBy and sortByDesc — reorder
sort() / sortDesc().
each — run side effects
reduce — fold to a single value
reduceInto — accumulate into an object
Like reduce, reduceInto folds a collection into a result, but its callback does not need to return a value. It is useful when mutating an existing object directly.
&):
chunk — split into batches
chunkBy — chunk by adjacent values
The chunkBy method breaks the collection into multiple, smaller collections by grouping adjacent items that share the same value for a given key or callback. For example, you may group adjacent products that share the same parent:
groupBy, items with the same value that are not adjacent are placed in separate chunks:
splitIn — split into a given number of groups
While chunk takes a group size, splitIn takes the number of groups. The groups other than the last are filled first.
skipUntil / skipWhile / takeUntil / takeWhile — slice by condition
skipUntil skips items until the condition is met, and skipWhile skips items while the condition is met. takeUntil / takeWhile do the opposite: they take items until the condition is met, or while it is met. You can also pass a value instead of a callback.
flatMap — map then flatten one level
Method chaining
The real power of collections is composing transformations:Working with Eloquent collections
Eloquent returnsIlluminate\Database\Eloquent\Collection from get(), which extends the base Collection and adds a few extras:
Lazy collections
LazyCollection uses PHP generators to process one item at a time, keeping memory flat regardless of how many records are in the result set.
Both Collection and LazyCollection implement the Illuminate\Support\Enumerable contract, so almost every method on this page works on a LazyCollection as is.
Creating a lazy collection
Chaining on a lazy collection
Lazy collection methods are evaluated on demand, so you can chainfilter(), map(), and each() without pulling everything into memory:
cursor() keeps a single database connection open for the duration of the loop. For very long-running loops, consider using chunk() to release and re-acquire the connection between batches.