Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Art's avatar
Level 1

Why is Eloquent collection different from Support collection, and how to use them interchangebly

In docs it is stated that Eloquent collection uses Support collection, and therefore supports many methods from it. What i can't get is why Eloquent collection uses objects to access values, while Support collections uses arrays.

Illuminate\Support\Collection {#3013
     all: [
       {#2990
         +"id": 1,
       },
     ],
   }

vs

Illuminate\Support\Collection {#2994
     all: [
       "id" => 1,
     ],
   }

I know that i can use map() to make new Support collection from Eloquent collection, but it seems to be an overkill. There are lots of times when i want to manipulate existing eloquent collection with some values, not coming from database.

Is there easy way to unify Support and Eloquent collections so they can have the same data types without additional manipulations?

0 likes
2 replies
D9705996's avatar
D9705996
Best Answer
Level 51

Eloquent Collection extends the Support Collection to add more Database related functionality that isn't applicable to non-database queries e.g. whereIn.

You can use functions like ->get() to return an eloquent collection that you can then work with as a regular collection as per this very contrived example which mixes and matches

User::where('user_id','>',100)
    ->get() 
    ->filter(function($user) {
        return $user->active;
    })
    ->pluck('name')
    ->random();

Before get you are working with the Query Builder.

https://laravel.com/docs/5.7/eloquent-collections

3 likes
Art's avatar
Level 1

@d9705996 Oh i see, thanks. pluck('name') from your example converts Eloquent to Support Collection

And here is excerpt from Laravel docs:

"While most Eloquent collection methods return a new instance of an Eloquent collection, the pluck, keys, zip, collapse, flatten and flip methods return a base collection instance. Likewise, if a map operation returns a collection that does not contain any Eloquent models, it will be automatically cast to a base collection."

P.S. thought pluck() only can be used with one field, so if there is a need to retrieve more columns, i'd still have to do it manually with map()

Please or to participate in this conversation.