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 ;)
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, whenUser::get()is much faster?
Best regards Alexander Bösch - sirthxalot
@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.
Please or to participate in this conversation.