0
0
Laravelframework~10 mins

Query optimization in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select all users with active status using Eloquent.

Laravel
$users = User::where('status', [1])->get();
Drag options to blanks, or click blank then click option'
A'pending'
B'deleted'
C'inactive'
D'active'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong status string like 'inactive' or 'pending'.
2fill in blank
medium

Complete the code to eager load the 'posts' relationship to optimize queries.

Laravel
$users = User::with([1])->get();
Drag options to blanks, or click blank then click option'
A'posts'
B'followers'
C'likes'
D'comments'
Attempts:
3 left
💡 Hint
Common Mistakes
Loading unrelated relationships like 'comments' or 'likes'.
3fill in blank
hard

Fix the error in the query to count users with more than 5 posts.

Laravel
$count = User::has('posts', '>', [1])->count();
Drag options to blanks, or click blank then click option'
A1
B5
C10
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong number like 3 or 10 which changes the condition.
4fill in blank
hard

Fill both blanks to optimize the query by selecting only 'id' and 'name' columns.

Laravel
$users = User::select([1], [2])->get();
Drag options to blanks, or click blank then click option'
A'id'
B'email'
C'name'
D'password'
Attempts:
3 left
💡 Hint
Common Mistakes
Selecting 'email' or 'password' which are not needed here.
5fill in blank
hard

Fill all three blanks to build a query that filters users by age, orders by name, and limits results.

Laravel
$users = User::where('age', '>', [1])->orderBy([2], [3])->get();
Drag options to blanks, or click blank then click option'
A18
B'name'
C'asc'
D'desc'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong age number or ordering direction.