Challenge - 5 Problems
Laravel Lazy Loading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output count of queries executed?
Given the following Laravel Eloquent code, how many database queries will be executed?
use App\Models\Post;
$posts = Post::all();
foreach ($posts as $post) {
echo $post->comments->count();
}Laravel
use App\Models\Post;
$posts = Post::all();
foreach ($posts as $post) {
echo $post->comments->count();
}Attempts:
2 left
💡 Hint
Think about how Laravel loads related models when accessed without eager loading.
✗ Incorrect
The code first fetches all posts in one query. Then, for each post, accessing comments triggers a separate query. This results in 1 query for posts plus N queries for comments, totaling N+1 queries.
📝 Syntax
intermediate2:00remaining
Which code snippet correctly prevents N+1 problem using eager loading?
Select the Laravel Eloquent code that correctly uses eager loading to prevent the N+1 query problem when retrieving posts with their comments.
Attempts:
2 left
💡 Hint
Eager loading is done before retrieving the data, not after.
✗ Incorrect
Option A uses with('comments') before get(), which eager loads comments in one query. Option A calls load() on a collection, which works but is less efficient than eager loading before retrieval. Option A is invalid syntax because with() cannot be called after get(). Option A uses a non-existent method.
🔧 Debug
advanced2:00remaining
Why does this eager loading code still cause N+1 queries?
Consider this Laravel code:
Why does this still cause N+1 queries?
$posts = Post::with('comments')->get();
foreach ($posts as $post) {
echo $post->comments()->where('approved', true)->count();
}Why does this still cause N+1 queries?
Laravel
$posts = Post::with('comments')->get(); foreach ($posts as $post) { echo $post->comments()->where('approved', true)->count(); }
Attempts:
2 left
💡 Hint
Check how the comments relationship is accessed inside the loop.
✗ Incorrect
Using comments() calls the relationship method returning a query builder, so each count() triggers a new query. The eager loaded comments are accessed via the property comments without parentheses.
🧠 Conceptual
advanced2:00remaining
How to eager load nested relationships to prevent N+1?
You want to load posts with their comments and each comment's author to avoid N+1 queries. Which eager loading syntax is correct?
Attempts:
2 left
💡 Hint
Nested relationships are specified with dot notation inside with().
✗ Incorrect
Option C correctly eager loads comments and their author in one query. Option C tries to load author directly on posts, which is incorrect. Option C loads author on posts separately, not nested. Option C uses load() after get(), which is less efficient.
❓ state_output
expert2:00remaining
What is the output count of queries executed with conditional eager loading?
Given this Laravel code:
How many queries are executed?
$posts = Post::with(['comments' => function ($query) {
$query->where('approved', true);
}])->get();
foreach ($posts as $post) {
echo $post->comments->count();
}How many queries are executed?
Laravel
$posts = Post::with(['comments' => function ($query) { $query->where('approved', true); }])->get(); foreach ($posts as $post) { echo $post->comments->count(); }
Attempts:
2 left
💡 Hint
Constrained eager loading performs 1 query for posts and 1 query for filtered comments.
✗ Incorrect
The with() method with a closure applies a condition to eager load only approved comments. This results in 1 query for posts + 1 query for approved comments, totaling 2 queries. Accessing $post->comments uses the already loaded data, so only 2 queries total.