0
0
Laravelframework~20 mins

Lazy loading and N+1 prevention in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Lazy 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 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();
}
AN+1 queries, where N is the number of posts
B1 query
CN queries, where N is the number of posts
D2 queries
Attempts:
2 left
💡 Hint
Think about how Laravel loads related models when accessed without eager loading.
📝 Syntax
intermediate
2: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.
A$posts = Post::with('comments')->get();
B$posts = Post::all()->load('comments');
C$posts = Post::get()->with('comments');
D$posts = Post::withComments()->get();
Attempts:
2 left
💡 Hint
Eager loading is done before retrieving the data, not after.
🔧 Debug
advanced
2:00remaining
Why does this eager loading code still cause N+1 queries?
Consider this Laravel code:

$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();
}
ABecause with('comments') does not eager load comments
BBecause comments() is called as a query each time, ignoring eager loaded data
CBecause count() cannot be used on eager loaded relationships
DBecause the where clause is invalid syntax
Attempts:
2 left
💡 Hint
Check how the comments relationship is accessed inside the loop.
🧠 Conceptual
advanced
2: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?
A$posts = Post::with('comments')->with('author')->get();
B$posts = Post::with(['comments', 'author'])->get();
C$posts = Post::with('comments.author')->get();
D$posts = Post::with('comments')->load('author');
Attempts:
2 left
💡 Hint
Nested relationships are specified with dot notation inside with().
state_output
expert
2:00remaining
What is the output count of queries executed with conditional eager loading?
Given this Laravel code:

$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();
}
A1 query
BN+1 queries, where N is the number of posts
C3 queries
D2 queries
Attempts:
2 left
💡 Hint
Constrained eager loading performs 1 query for posts and 1 query for filtered comments.