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

davidyu's avatar

What is the purpose of the `all` key in an eloquent get query

Say I have the following query Bed::where('user_id', 1)->get()

     all: [
       App\Bed {#796
         id: 1,
         user_id: 1,
         name: "sgdffd",
         gender: "male",
         type: "single",
         status: "available",
         created_at: "2018-03-19 07:53:51",
         updated_at: "2018-03-19 07:53:51",
       },
     ],

whats the point of the all key wouldn't it be simpler to just return an array of model data?

     [
       App\Bed {#796
         id: 1,
         user_id: 1,
         name: "sgdffd",
         gender: "male",
         type: "single",
         status: "available",
         created_at: "2018-03-19 07:53:51",
         updated_at: "2018-03-19 07:53:51",
       },
     ],
0 likes
4 replies
bobbybouwmann's avatar

That is how collections work at the moment and it's just a presentation of the data. However when you access the data you will never see the all key at all!

davidyu's avatar

But what happens if I were to return that data straight away back tothe user say return Bed::where('user_id', 1)->get() wouldn't that all key end up in the returned data?

Helmchen's avatar

just do it and see what you get :)

Laravel turns it into json and returns it

you can do the same by calling ->toJson() or ->toArray() on the collection

1 like
bobbybouwmann's avatar
Level 88

Yeah, Laravel will automatically convert it to the right format. So for your views to an array and for api responses to json using that array.

Let me know if you have more questions!

Please or to participate in this conversation.