0
0
Laravelframework~20 mins

Eager loading (with) in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Eager Loading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Laravel eager loading code?
Given the following Eloquent query, what will be the number of queries executed when fetching posts with their comments eagerly loaded?

Post::with('comments')->get();
A1 query executed: one for posts and their comments together
B2 queries executed: one for posts and one for comments
CMultiple queries executed: one for each post's comments
DNo queries executed until comments are accessed
Attempts:
2 left
💡 Hint
Eager loading loads related models in separate queries before accessing them.
📝 Syntax
intermediate
2:00remaining
Which option correctly eager loads nested relationships in Laravel?
You want to eager load the 'comments' relationship and, within comments, the 'author' relationship. Which code snippet is correct?
APost::with('comments->author')->get();
BPost::with(['comments', 'author'])->get();
CPost::with('comments.author')->get();
DPost::with('comments')->with('author')->get();
Attempts:
2 left
💡 Hint
Use dot notation to eager load nested relationships.
🔧 Debug
advanced
2:00remaining
Why does this eager loading code cause an error?
Consider this code:

Post::with(['comments' => function($query) { $query->where('approved', true); }])->get();

What is the cause of the error if 'comments' is not a defined relationship on Post?
AThe 'approved' column does not exist in the comments table, causing a database error.
BThe closure syntax inside 'with' is invalid and causes a syntax error.
CThe 'where' clause inside the closure is not allowed in eager loading.
DThe 'comments' relationship is not defined on the Post model, causing a runtime error.
Attempts:
2 left
💡 Hint
Check if the relationship method exists on the model.
state_output
advanced
2:00remaining
What is the output count of comments when eager loading with a constraint?
Given this code:

$posts = Post::with(['comments' => fn($q) => $q->where('approved', true)])->get();
$count = $posts->first()->comments->count();


What does $count represent?
AThe number of approved comments for the first post
BThe total number of comments for all posts
CThe total number of comments for the first post, ignoring approval
DAlways zero because eager loading with constraints disables comments
Attempts:
2 left
💡 Hint
Eager loading constraints filter the related models loaded.
🧠 Conceptual
expert
2:00remaining
Why prefer eager loading over lazy loading in Laravel?
Which is the main reason to use eager loading (with) instead of lazy loading when fetching related models in Laravel?
ATo reduce the number of database queries and improve performance
BTo load related models only when accessed to save memory
CTo automatically cache related models for offline use
DTo force Laravel to use raw SQL queries instead of Eloquent
Attempts:
2 left
💡 Hint
Think about how many queries run when accessing related data.