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

eggplantSword's avatar

Using query() in Eloquent

What difference does it make to use this when making a query to the db? I have a coworker who always puts this is his queries but I don't think it's needed as the query is executed the same with or without it. for example:

What is the difference in these:

 $var= Model::query()->findOrFail($id);

 $var= Model::findOrFail($id);

I'm just wondering what is the point of doing this and for what?

0 likes
4 replies
automica's avatar

@msslgomez in your case, it makes no difference, but if you were chaining then its neater and more readable.

eg:

$var = Model::query()
    ->where('some-field','some-value')
    ->where('some-field2','some-value2')
    ->sortAsc()
    ->get();
eggplantSword's avatar

@automica it does look nicer but I can omit that and my code would be shorter, does it make a difference in performance?

automica's avatar

@msslgomez no difference in performance as eloquent is converted to sql before its ran anyway.

tykus's avatar

There is no difference.

The query method does have the advantage, when building larger queries, of IDE completions because it returns the Eloquent Builder instance. Some devs will also take advantage of query to align multi-line queries for readability

Also checkout this Codecourse short series

Please or to participate in this conversation.