Courses

Laravel 12 Eloquent: Expert Level

Eager Loading with load() and $with

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Use load() method on the Eloquent Collection for eager loading after retrieval
- Model's $with property auto-loads relationships
- $with can cause performance issues by loading unnecessary data
- Always check queries with Debugbar

Video Version of the Lesson

[Only for premium members]

Link to the repository

[Only for premium members]

In the previous lessons, we discussed how to solve N+1 query problems using eager loading with the with() method directly in your query. However, there are situations where you might need alternative approaches to eager loading. This lesson covers those techniques.

Let's return to our original example of viewing posts with their authors. As we've seen, without eager loading, this produces 31 queries - one for posts and 30 separate queries for users.


Loading Relationships After Initial Query

Sometimes you might receive an Eloquent collection from another part of your application, perhaps from code written by another developer, and you don't have direct access to modify the original query. In these situations, you can load relationships dynamically after...

The full lesson is only for Premium Members.
Want to access all 38 video+text lessons of this course? (1 h 34 min)

You also get:

  • 77 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord