Laravel Collections: all(), toArray(), and values() methods
Laravel Collections make handling arrays elegant, expressive, and fluent. Some methods like all(), toArray(), and values() seem similar, and sometimes we mistakenly use them interchangeably, yet they behave quite differently when you look under the hood.
I recently was debugging an issue with Filament actions, and realised that these are not the same:
/******* Scenario #1 *******/ $actions = $this->getActions(); $actions = collect($actions)->map->someMethod()->all(); dump($actions); // --> $actions is what I expected it to be /******** Scenario #2 ********/ $actions = collect($actions)->map->someMethod()->toArray(); dump($actions); // --> Let's just call this an "$action" film
In this article, we’ll look at how these three methods differ, when to use each, and how they impact your data shape and output.
1. all()
It returns the underlying array of the collection exactly as it is, preserving keys and object instances.
$users = collect([ 10 => (object) ['name' => 'John'], 11 => (object) ['name' => 'Jane'], ]); $result = $users->all(); dd($result);
Output:
array:2 [
10 => { name: "John" },
11 => { name: "Jane" }
]
Common usages:
- You need the exact same array structure and key mapping.
- You’re not converting to JSON or serializing data.
2. toArray()
Unlike all(), toArray() recursively converts objects and nested collections into plain arrays.
$users = collect([ ['id' => 1, 'roles' => collect(['admin', 'editor'])], ]); $result = $users->toArray(); dd($result);
Output:
array:1 [
0 => [
"id" => 1,
"roles" => [
0 => "admin",
1 => "editor"
]
]
]
Common usages:
- You’re preparing response data (e.g., for APIs or JSON).
- You want to deeply convert collections/objects to arrays.
Note:
Any Arrayable item in the collection is converted to an array, like in the example I gave in this article's introduction with Filament Actions.
3. values()
values() doesn’t convert or unwrap anything — it simply resets array keys to start from zero while keeping values intact.
$users = collect([ 5 => 'Alice', 7 => 'Bob', ])->values(); dd($users->all());
Output:
array:2 [ 0 => "Alice", 1 => "Bob" ]
Common usages:
- You filtered or modified a collection and need clean, sequential indexes.
- You’re returning lists (e.g., arrays for JSON) and want consistent ordering.
Note:
values() returns a new collection, not an array. Use ->all() or ->toArray() if you need a plain array.
Conclusion
The all() and toArray() methods return array. The values() still gives you back a collection object().
all()→ returns the raw array, untouched.toArray()→ deeply converts anythingArrayableinto arrays.values()→ resets numeric keys for a clean, sequential list.
And don't forget to write your tests! Unintended Collection method usage can usually be caught easily with a good test suite. I recently wrote about writing fast Laravel tests.
You may also check the Collections' documentation for other awesome and fluent methods for handling data collection in your Laravel app.
Thanks for stopping by!
