Laravel Eloquent relationships with eager loading

11813
0

Eloquent ORM makes working with database relationships intuitive and powerful. I define relationships using methods like belongsTo, hasMany, and belongsToMany that return query builders. The beauty of Eloquent is lazy loading—relationships load only when accessed. However, this causes N+1 queries in loops. The with() method eager loads relationships in a single query, dramatically improving performance. I use load() to eager load after retrieving models, and withCount() to get relationship counts without loading full models. Nested eager loading via dot notation handles deep relationships. The when() method conditionally eager loads based on request parameters. Proper eager loading is essential for performant Laravel applications.