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

sirhxalot's avatar

Use all() method or get() to fetch all entries?

Hi folks!

I have heard that the get() method (from Query Builder) is much more performant than the all() method from Eloquent Model is. So I tried to this out and indeed the Query Builder solution is much more faster.

So my first thought was there must be a benefit when I use the Eloquent solution - since its more than 3x times slower than the Query Builder. But I didn't find any benefit to use the all() method.

As far as I heard there are some traits and other stuff that will be used for the Eloquent Model, but is there any benefit? I didn't found anything even mutators are working exact the same.

Why should I ever use User::all() to fetch all entries, when User::get() is much faster?

Best regards Alexander Bösch - sirthxalot

0 likes
8 replies
stefanbauer's avatar

I guess it does the same thing. all is on the model and you cannot modify the query at all. While get is on the builder and you can modify your query. I use always get ;)

sirhxalot's avatar

Thanks @stefanbauer!

Do you know where I can found more background information what is going on with this topic - Why is Eloquent that slow? Is it because of traits? And is there really no benefit?

I am just curios and it wont let me alone ;)

stefanbauer's avatar

Check the code :)

  • Illuminate\Database\Eloquent\Builder (get())
  • Illuminate\Database\Eloquent\Model (all())
sirhxalot's avatar

Yea I did. And maybe I missunderstanding something.

My intepretation how all() works:

  1. A new Eloquent Model will be instanciated
  2. The Query Builder will be attached to the model (but why not use the Query Builder directly?)
  3. The get() method will be triggered fetching all [*] rows

But why does this takes longer? Answer: Eloquent Model - Right?

Do I ever have a benefit from using Eloquent? Because I just see two downpoints:

  • slower
  • query can not be modified

Hope you can follow appreciate your quick response by the way.

sirhxalot's avatar

Because I would expect that for example the Eloquent solution maybe loading mutators and observers or other Eloquent related stuff where the Query Builder wouldn't -

BUT THAT's NOT THE CASE!!!

alielkhateeb's avatar
Level 1

@sirhxalot Hi

I did some research and I found a benefit of using Eloquent.

Eloquent is an ORM, which means it can automatically handle the relationships of your models for you. So basically I assume this means that if you have a foreign key in your table, and you retrieved data from this table with all(), you can load the related Model instance (that the foreign key is pointing to), on the other hand, I suppose using get() you'll have to write a bit more complex queries to retrieve the related models.

Note: I just read a little bit but I haven't tried what I mentioned above, but it does make sense why get() is faster than all()

I suggest you use all() when you know you'll need the related tables, and use get() if you are sure you won't need the related tables.

Hope that helps.

UPDATE:

after reading the Laravel Relationship Docs

$users = User::with('podcasts')->get();

This code will retrieve users with related podcasts so I assume all() does this by default. If someone knows better please correct me if I'm wrong.

jlrdw's avatar

Yes a lot is happening in the background with eloquent but if you study, eloquent yes is a shortcut however it gets converted to regular SQL at runtime. Basically active record does indeed take longer especially on larger results.

I use it at times but for complex stuff with joins Etc I just use normal SQL and pdo with getPdo().

Snapey's avatar

How did you come to the conclusion that get() was three times quicker than all()? - that does not make any sense.

Please or to participate in this conversation.