collect
Create a Collection from an array, iterable, Arrayable or null.
collect(\Illuminate\Contracts\Support\Arrayable|iterable|null $value = []): \Illuminate\Support\CollectionParameters
| Parameter | Type | Description |
|---|---|---|
$valueoptional | \Illuminate\Contracts\Support\Arrayable|iterable|null= [] | The value to wrap. Arrays keep their keys, Arrayable/Traversable values are converted to an array first, null becomes an empty collection, and anything else is cast to an array. |
Return value
\Illuminate\Support\CollectionA Collection wrapping the given value. Passing null produces an empty Collection.
collect() is the doorway to the Collection API. Once a value is inside a Collection you get map, filter, groupBy, pluck, sum and the rest as a chain, instead of nested array_map/array_filter calls with temporary variables in between.
What it accepts
- An array — wrapped as-is, keys preserved.
null— you get an empty collection, not a collection holdingnull. This is the single most useful property of the helper:collect($maybeNull)->each(...)is always safe.- Arrayable, Jsonable, JsonSerializable or Traversable — converted to an array first.
- A plain object — its public properties become the items.
- Anything else — cast to an array, so a scalar ends up as a one-element collection.
When to reach for it
Reach for it as soon as a transformation takes more than one step, or when a value's shape is uncertain and you want a uniform iterable. It costs one object allocation and buys you a null-safe, chainable API.
Watch the eagerness
A generator passed to collect() is drained immediately into an array. For a large or infinite source use LazyCollection::make() instead, which keeps the generator lazy.
Example
When to use it
- Chain map/filter/groupBy over a plain array without intermediate variables.
- Normalise a value that may be an array, a single item or null before looping over it.
- Wrap a decoded JSON payload so you can pluck and keyBy the rows.
Frequently asked questions
What is the difference between collect() and new Collection()?
None in behaviour — collect() is a thin wrapper around new Illuminate\Support\Collection($value). The helper exists so you can write it inline without importing the class, and so the call reads well at the start of a chain.
Does collect(null) throw an error?
No, it returns an empty collection. Null is explicitly special-cased, which is why collect($maybeNull)->each(...) is a safe pattern and why you rarely need a null guard before wrapping a value.
How do I get a plain array back out of a collection?
Call ->all() for the items exactly as stored, or ->toArray() to also convert any nested Arrayable items (such as Eloquent models) into arrays. ->all() is the one you want when the items are already scalars.
More examples
Grouping and summing rows
groupBy returns a collection of collections, so the map closure receives a Collection and can call sum() on it directly. sortKeys() is there for determinism — group order otherwise follows first appearance in the input.
<?php
$orders = [
['customer' => 'ana', 'total' => 30],
['customer' => 'ben', 'total' => 12],
['customer' => 'ana', 'total' => 18],
];
$byCustomer = collect($orders)
->groupBy('customer')
->map(fn ($rows) => $rows->sum('total'))
->sortKeys()
->all();
print_r($byCustomer);
// Array ( [ana] => 48 [ben] => 12 )Absorbing a nullable payload
Shows the idiom that makes collect() worth reaching for: a missing branch of a payload flows through pluck() and out as an empty array rather than blowing up on a null dereference.
<?php
$payload = ['data' => null];
// data_get returns null, collect() turns that into an empty collection,
// so the chain runs without a single null check.
$ids = collect(data_get($payload, 'data'))
->pluck('id')
->all();
var_dump($ids);
// array(0) {}In a controller, on an Eloquent result
DB::select() hands back an array of stdClass rows, not a Collection — this is the common case where collect() earns its keep. Calling it on an Eloquent result is redundant, because get() already returns a Collection.
<?php
namespace App\Http\Controllers;
use App\Models\Invoice;
class InvoiceController extends Controller
{
public function index()
{
// Eloquent already returns a Collection, so collect() here is
// only needed when you hold a raw array from DB::select().
$rows = \DB::select('select status, amount from invoices');
return collect($rows)
->groupBy('status')
->map(fn ($group) => $group->sum('amount'));
}
}
Discussion