Show the code.
Eager Loading = Query performance?
Hey, I guess I get the concept of Eager Loading but I'm not sure if it should improve the query performance. I try to retrieve a collection with some relations and it makes the same amount of queries using Eager Loading or not.
Maybe I am not doing it right, or.. it is not what it should do.
Thanks.
Your examples still don't show a one to one. A comment belongsTo a user, while the inverse is hasMany, not hasOne.
It works exactly the same way. Replace comment by user and author by profile in my example above if it helps you understand. If it doesn't work this way in your code there is a problem with it.
$user = User::with('profile')->first();
echo $user->profile;
Two queries
$user = User::first();
echo $user->profile;
Two queries
$users = User::with('profile')->get();
foreach ($users as $user) {
echo $user->profile;
}
Two queries
$users = User::all();
foreach ($users as $user) {
echo $user->profile;
}
1 + (number of users) queries.
@GastonUy show us some code. You can always have a constant number of queries but sometimes it is misleading :)
E.g. Passenger belongs to Flight and also belongs to Group, should I define this with polymorphic or it is not necessary? Thought that maybe it could be the reason.
No it is not necessary at all and I feel it is not suited here. $passengers = Passenger::with('flight', 'group')->get() should work as expected.
Please or to participate in this conversation.