Collection::all

Returns the collection's underlying PHP array, with its keys intact.

Signature
all(): array
  • Added in Laravel 4.0

Return value

array<TKey, TValue>The raw array the collection wraps, keys preserved exactly as they are.

all() is the way out of the fluent API. It hands you the array the collection has been wrapping the whole time — no copying of nested structures, no conversion, no reindexing.

When to reach for it

Reach for it at the boundary: passing data to a function that type-hints array, feeding implode(), returning from a method whose contract is an array, or writing an assertion in a test.

all() versus toArray()

This is the distinction people miss. all() returns the items exactly as stored — a collection of Eloquent models comes back as an array of models. toArray() recurses, calling toArray() on every Arrayable item, so the same collection comes back as an array of arrays. If you want the objects, use all(). If you want a serialisable structure, use toArray().

Keys survive

Methods such as filter(), reject() and where() preserve keys, so the array you get back can be sparse: [0 => 'a', 2 => 'c']. json_encode() turns that into a JSON object, not an array, which is a common source of broken front-end code. Call values() before all() when the consumer expects a list.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Pass a filtered result to a function that type-hints a plain array.
  • Feed collection values to implode(), array_sum() or another native array function.
  • Return the raw Eloquent model objects from a query without serialising them to arrays.

Frequently asked questions

What is the difference between all() and toArray() on a Laravel collection?

all() returns the items exactly as stored, while toArray() recursively converts every Arrayable item into an array. A collection of Eloquent models gives you model objects from all() and nested associative arrays from toArray().

Why does my collection turn into a JSON object instead of an array?

Because filter(), reject() and where() preserve the original keys, leaving gaps such as [0, 2, 5]; json_encode() serialises a non-sequential array as an object. Call values() before all() to reindex from zero.

Does all() copy the array or return a reference?

It returns the internal array by value, so modifying the returned array does not change the collection. Objects inside it are still the same instances, so mutating an object does affect the collection.

More examples

all() keeps models, toArray() flattens them

Both calls return an array, but only all() leaves the items untouched. toArray() recurses into every Arrayable item, so the model objects become associative arrays and property access stops working. Choose based on whether the consumer needs objects or plain data.

Example · php
<?php

$users = User::where('active', true)->get();

// all() gives you the User objects themselves.
$models = $users->all();
echo get_class($models[0]), PHP_EOL;
// App\Models\User

// toArray() walks every item and serialises it.
$rows = $users->toArray();
var_dump(is_array($rows[0]));
// bool(true)

echo $models[0]->email, PHP_EOL;   // works
// echo $rows[0]->email;            // Attempt to read property on array

Sparse keys and JSON

filter() preserved the original keys 1 and 3. Because the array is no longer a packed list, json_encode() serialises it as an object. Inserting values() renumbers the keys from zero and restores the array shape the API consumer expects.

Example · php
<?php

$scores = collect([50, 90, 40, 95]);

$passing = $scores->filter(fn (int $s) => $s >= 60);

print_r($passing->all());
// Array ( [1] => 90 [3] => 95 )

echo json_encode($passing->all()), PHP_EOL;
// {"1":90,"3":95}     <- an OBJECT

echo json_encode($passing->values()->all()), PHP_EOL;
// [90,95]             <- an ARRAY

Discussion

  • Be the first to comment on this lesson.