I really tried to understand the difference between the with()
method and the load()
method, but couldn't really understand.
As I see it, using the with()
method is "better" since I eager load the relation. It seems that if I use load()
I load the relation just as if I would use the hasMany()
(or any other method that relates to the relation between objects).
Do I get it wrong?
with() function is used to eager load in Laravel. Unless of using 2 or more separate queries to fetch data from the database , we can use it with() method after the first command. It provides a better user experience as we do not have to wait for a longer period of time in fetching data from the database.
The main difference between eager and lazy loading is eager loading get all data with relationship records in single query and lazy loading require N+1 queries for getting main model and relation data. Eager loading run single query whereas lazy loading run N+1 queries.
Laravel eager loading. What is eager loading? Eager loading is a concept in which when retrieving items, you get all the needed items together with all (or most) related items at the same time. This is in contrast to lazy loading where you only get one item at one go and then retrieve related items only when needed.
1 Lazy Loading : In Laravel Lazy Loading can be seen on two ways but both are nuance. Let us suppose a relationship: Users and Posts. a User can have multiple Posts. a Post can have single User. Here we can say User is parent table for Posts.
Both accomplish the same end results—eager loading a related model onto the first. In fact, they both run exactly the same two queries. The key difference is that with()
eager loads the related model up front, immediately after the initial query (all()
, first()
, or find(x)
, for example); when using load()
, you run the initial query first, and then eager load the relation at some later point.
"Eager" here means that we're associating all the related models for a particular result set using just one query, as opposed to having to run n queries, where n is the number of items in the initial set.
Eager loading using with()
If we eager load using with()
, for example:
$users = User::with('comments')->get();
...if we have 5 users, the following two queries get run immediately:
select * from `users` select * from `comments` where `comments`.`user_id` in (1, 2, 3, 4, 5)
...and we end up with a collection of models that have the comments attached to the user model, so we can do something like $users->comments->first()->body
.
"Lazy" eager loading using load()
Or, we can separate the two queries, first by getting the initial result:
$users = User::all();
which runs:
select * from `users`
And later, if we decide that we need the related comments for all these users, we can eager load them after the fact:
$users = $users->load('comments');
which runs the 2nd query:
select * from `comments` where `comments`.`user_id` in (1, 2, 3, 4, 5)
...and we end up with the same result, just split into two steps. Again, we can call $users->comments->first()->body
to get to the related model for any item.
Why use load()
vs. with()
? load()
gives you the option of deciding later, based on some dynamic condition, whether or not you need to run the 2nd query. If, however, there's no question that you'll need to access all the related items, use with()
.
The alternative to either of these would be looping through the initial result set and querying a hasMany()
relation for each item. This would end up running n+1 queries, or 6 in this example. Eager loading, regardless of whether it's done up-front with with()
or later with load()
, only runs 2 queries.
As @damiani said, Both accomplish the same end results—eager loading a related model onto the first. In fact, they both run exactly the same two queries. The key difference is that with() eager loads the related model up front, immediately after the initial query (all(), first(), or find(x), for example); when using load(), you run the initial query first, and then eager load the relation at some later point.
There is one more difference between With() & load(), you can put the conditions when using with() but you can't do the same in case of load()
For example:
ProductCategory::with('children') ->with(['products' => function ($q) use($SpecificID) { $q->whereHas('types', function($q) use($SpecificID) { $q->where('types.id', $SpecificID) }); }]) ->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With