0
0
Laravelframework~20 mins

Select queries in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Select Queries Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a basic select query
What will be the output of this Laravel Eloquent query if the users table has 3 records with names 'Alice', 'Bob', and 'Charlie'?
Laravel
$users = User::select('name')->get();
return $users->pluck('name')->toArray();
A["Alice", "Bob", "Charlie"]
B[{"name": "Alice"}, {"name": "Bob"}, {"name": "Charlie"}]
C["Alice Bob Charlie"]
D[]
Attempts:
2 left
💡 Hint
Remember that pluck extracts the values of a single column into an array.
component_behavior
intermediate
2:00remaining
Behavior of select with multiple columns
Given this Laravel query, what will be the structure of each item in the returned collection?
Laravel
$users = User::select('id', 'email')->get();
return $users->first();
AAn array with 'id' and 'email' keys
BAn object with all user properties including 'id' and 'email'
CAn object with only 'id' and 'email' properties
DA string containing 'id' and 'email'
Attempts:
2 left
💡 Hint
Selecting specific columns limits the attributes loaded on the model.
📝 Syntax
advanced
2:00remaining
Correct syntax for selecting with alias
Which option correctly selects the 'name' column as 'username' using Laravel's query builder?
ADB::table('users')->select(['name as username'])->get();
BDB::table('users')->select('name')->as('username')->get();
CDB::table('users')->select(DB::raw('name as username'))->get();
DDB::table('users')->select('name as username')->get();
Attempts:
2 left
💡 Hint
Column aliases can be specified directly like 'name as username' in select().
🔧 Debug
advanced
2:00remaining
Identify the error in this select query
What error will this Laravel query produce?
Laravel
$users = User::select('id', 'email')->where('name', '=', 'Alice')->get();
return $users->email;
AError: Property 'email' does not exist on Collection
BError: Undefined variable 'users'
CNo error, returns email of first user
DError: Missing semicolon
Attempts:
2 left
💡 Hint
Remember what type is returned by get() method.
🧠 Conceptual
expert
3:00remaining
Effect of select on eager loading relationships
If you run this query, what will be the effect on the eager loaded 'posts' relationship?
Laravel
$users = User::select('id', 'name')->with('posts')->get();
AThe 'posts' relationship will not load because select limits the whole query
BThe 'posts' relationship will load all columns for posts as usual
CThe 'posts' relationship will only load 'id' and 'name' columns from posts
DThe query will throw an error because select conflicts with with()
Attempts:
2 left
💡 Hint
Eager loading runs separate queries for relationships.