Array, collection and eloquent collection in Laravel
Published by Andrés López

When I started programming in Laravel (which was also when I started coding in PHP), I remember writing code without much understanding of what I was doing, but somehow it worked. I didn't know the difference between data types or objects; to me, everything was the same.
I recall trying to access array properties when connecting to APIs:
$data = json_decode($response);
$data[0]['name']; // FATAL ERROR
Or calling the toArray
method (I didn't even know where it came from) just to work with arrays:
$users = DB::table('users')->get()->toArray();
$users[0]['name'];
Later, I learned that if you called json_decode
with the second parameter as true
, you could access values using the
notation, and I ended up using Eloquent models in a very weird and ugly way 🤢:
$user = json_decode(json_encode(User::first()), true);
$user['name'];
At some point, my code became unmaintainable. It was practically easier to refactor everything than try to modify it. The best thing I could do was read, read, and read some more, first the official Laravel documentation and some tutorials that I found online. My first step was differentiating between an array and an object.
I always had doubts about why sometimes this worked and other not:
$user = $response->product;
$user = $response['product'];
PHP Array and PHP Object
The main difference between these two data types is how you access their information.
On one hand, we have an Array:
$data = [];
$data['name']; // always accessed via []
And on the other, an Object:
$data = new stdClass(); // the 'official' way to create an Object
// There are many ways to create an object, but this is the most common
$data = json_decode($response);
$data->name; // always accessed via ->
In comparison, arrays start to fall short, usually you will use sequential programming (top to bottom).
This is where things get interesting, PHP has functions for everything, but some take parameters by value and others by reference, leaving us with very odd and less readable code:
// Clean the array to keep only the integers, sorted from smallest to largest
$data = [4, '', 0, 'test', 32];
$data = array_filter($data, fn($item) => is_numeric($item));
asort($data);
echo $data;
Laravel Collection
The Laravel community noticed this dilemma in PHP and began creating the Illuminate\Support\Collection
class to handle arrays. Here, we revisit the concept of an Object, as every time we use this class, we get an Object to interact with:
use Illuminate\Support\Collection;
$data = [4, '', 0, 'test', 32];
$data = Collection::make($data); // a very elegant way to create it
$data = collect($data); // and this is the shorthand helper
// both do the same thing
Now, using the methods of this class, which you can consult in the official documentation, it’s easier to process this information in a readable way:
// Clean the array to keep only the integers, sorted from smallest to largest
$data = [4, '', 0, 'test', 32];
echo Collection::make($data)
->filter(fn($item) => is_numeric($item))
->sort()
->all(); // this method is just to get the array and not the Object
Eloquent Collection
What about models? Well, here the story changes a bit. Every time you query the database using Eloquent models and use the ->get() method, you’ll get an Object of Illuminate\Database\Eloquent\Collection:
$users = User::where('name', 'LIKE', "{$search}%")->get();
To be honest, if you read the official documentation again, you’ll notice that you can use the same methods we reviewed earlier. However, there’s a bit of extra flavor in this new dish, and to understand it, we need to see how an Eloquent Collection is built.
- It’s an inheritance of
Illuminate\Support\Collection
:
namespace Illuminate\Database\Eloquent;
class Collection extends \Illuminate\Support\Collection
{
...
}
- We’re always dealing with a model Class per Eloquent Collection.
- There are new methods related to models.
- Models have Eloquent relationships.
- It’s a Collection focused on database interaction.
Considering the above, you’ll see there are new methods, which you can consult in the official documentation:
$users = User::get();
$user->load('comments'); // loads a relationship
echo $users->modelKeys(); // [1, 2, 3, 4, 5]
How do I differentiate between Array, Object, Collection, and Eloquent Collection?
The best answer I can give you is: practice. The more you program in Laravel and PHP, the easier it will be to differentiate between one data type and another. However, I can give you a quick checklist:
// Return instances of \Illuminate\Support\Collection
collect($data);
Collection::make($data);
DB::table($table)->get();
// Return instances of \Illuminate\Database\Eloquent\Collection
User::get();
$user->comments()->get();
$user->load('posts')->posts;
// Arrays, 100% sure
array(1, 2, 3, 4, 5);
[1, 2, 3, 4, 5];
// Maybe arrays, maybe objects
$values = json_decode($data, false);
$values = json_decode($data, true);
// Use this function to clear up doubts
gettype($values);
I hope this information has been helpful. I’ve been working with Laravel for many years, and this is the first time I’ve shared my knowledge. I have much more to share in future blog posts. Thanks! :)

Andrés López
Laravel lover, Vue enthusiast & writer of everything sounds interesting