Collections Introduction
A Collection wraps an array in a fluent, chainable object with dozens of helpful methods.
Syntax
collect([1, 2, 3])->methodName();The Illuminate\Support\Collection class is a fluent wrapper around arrays. Instead of nesting PHP array functions, you chain readable methods together.
Create a collection with the global collect() helper. Every method returns either a new collection (so you can keep chaining) or a final value.
Why use collections?
- Readable, chainable syntax.
- Dozens of built-in methods (map, filter, reduce, and more).
- Immutable by default — most methods return a new collection.
Example
Loading editor…
Press Run to execute the code.
When to use it
- A developer replaces a nested array_map/array_filter chain with a fluent Collection pipeline, making the data transformation logic readable at a glance.
- An API controller collects raw database rows into a Collection and chains pluck(), groupBy(), and map() to build the JSON response in a single expression.
- A reporting module wraps CSV-imported rows in a Collection so the rest of the codebase can use consistent methods regardless of the data source.
More examples
Create a collection from an array
Shows the two common ways to instantiate a Collection: via the class directly or the collect() helper.
use Illuminate\Support\Collection;
$items = collect([1, 2, 3, 4, 5]);
// Also available as a global helper
$items = collect(['apple', 'banana', 'cherry']);Chain multiple methods fluently
Chains unique(), filter(), sortDesc(), and values() without mutating intermediate arrays, showing the fluent API.
$result = collect([3, 1, 4, 1, 5, 9, 2, 6])
->unique()
->filter(fn($n) => $n > 3)
->sortDesc()
->values();
// => Collection [9, 6, 5, 4]Convert an Eloquent result to a collection
Demonstrates that Eloquent query results are already Collections, so you can immediately chain helper methods on them.
// Eloquent already returns a Collection
$users = User::where('active', true)->get();
// Chain collection methods directly
$emails = $users
->reject(fn($u) => $u->email_verified_at === null)
->pluck('email');
Discussion