What is the benefit of eager loading, when do you use it?

Technology CommunityCategory: LaravelWhat is the benefit of eager loading, when do you use it?
VietMX Staff asked 3 years ago

When accessing Eloquent relationships as properties, the relationship data is “lazy loaded”. This means the relationship data is not actually loaded until you first access the property. However, Eloquent can “eager load” relationships at the time you query the parent model.

Eager loading alleviates the N + 1 query problem when we have nested objects (like books -> author). We can use eager loading to reduce this operation to just 2 queries.