JKB's avatar
Level 1

What's Model::query() function ?

Hi,

Could someone explain me the difference between User::query() and User::all() ?

I can't found any documentation about the query function.

Thank you

0 likes
5 replies
topvillas's avatar

User::query() return a Builder object. User::all() returns all the records from the user table.

1 like
JKB's avatar
Level 1

Ok, thank you. Do you know where I can find informations about query() in official laravel documentation ?

As you can see, these notions (builder, collections, ORM ..) are not perfectly clear for me ;-)

And as I can't find query() in documentation, I was asking me if this fonction is still used in Laravel 5.5^, or if it it has be replaced by another concept, but still retrocompatible.

I think that it is a difficulty that can be met by learning Laravel, because there are still many examples or tutorials made with older versions, and sometimes using features that are no longer relevant. Not always easy to sort through these sources.

But that does not stop me from being totally in love with Laravel :)

topvillas's avatar

I couldn't find it in any guides, I had look at the API docs. I'd never heard of it before so I'd imagine it's use is limited.

1 like
skliche's avatar
skliche
Best Answer
Level 42

Usually, you don't need to call query() on an Eloquent model. The methods you usually use, like all or where, call it or trigger query() under the hood and either return a model instance, an Eloquent collection, or a query builder instance.

1 like
JKB's avatar
Level 1

Ok, so maybe I'm not lost: D Thank you for reassuring me.

FYI, this code comes from the documentation of a popular package yajra laravel-datatables .

Thank you!

Please or to participate in this conversation.