Use dot-notation strings inside with() to eager load deeply nested relationships in single database query chains.
When fetching models that require multi-level relationships (e.g. Posts -> Comments -> Author), use dot-notation strings in with() to eager load all levels efficiently.
use App\Models\Post;
// Eager loads post author, comments, and comment authors
$posts = Post::with(['author', 'comments.author'])->get();
- Eager loads multi-level nested relationships cleanly in dot-notation strings
- Prevents cascading N+1 query problems across nested views
- Allows applying constraints to nested levels using array key closures
Tags:
#Laravel #Eloquent #Relationships