Complete the code to eager load the 'posts' relationship for all users.
$users = User::[1]('posts')->get();
Using with('posts') tells Laravel to eager load the posts relationship, preventing N+1 queries.
Complete the code to lazy load the 'comments' relationship on an existing $post model.
$post->[1]('comments');
The load method is used on an existing model instance to lazy load relationships.
Fix the error in the code to prevent N+1 queries when accessing each user's posts count.
foreach ($users as $user) { echo $user->posts->count(); } // Fix by eager loading posts count: $users = User::[1]('posts')->get();
The withCount method eager loads the count of related models, preventing N+1 queries when counting posts.
Fill both blanks to eager load 'comments' and 'author' relationships for posts.
$posts = Post::[1](['comments', [2]])->get();
Use with to eager load multiple relationships. The 'author' relationship is commonly named for the user who wrote the post.
Fill all three blanks to eager load 'tags', count 'comments', and filter posts with more than 5 comments.
$posts = Post::[1]('tags')->[2]('comments')->having('[3]', '>', 5)->get();
Use with('tags') to eager load tags, withCount('comments') to get comments count, and filter with having('comments_count', '>', 5).