1. What is Eager Loading in Laravel?
By default, Laravel uses lazy loading for relationships. This means:
- If you fetch a model and then access its related models, Laravel will execute additional queries for each related model.
- This can cause the N+1 query problem, where N is the number of parent models.
Eager loading solves this by loading related models upfront in the same query, reducing the number of queries.
2. Example Scenario
Suppose we have:
- User model → has many Post models.
- Post model → belongs to User.
Models:
