0
0
Laravelframework~10 mins

Select queries 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 records from the 'users' table using Laravel's query builder.

Laravel
$users = DB::table('users')->[1]();
Drag options to blanks, or click blank then click option'
Aget
Bselect
Call
Dfetch
Attempts:
3 left
💡 Hint
Common Mistakes
Using select() instead of get() to fetch data.
Using all() which is not a query builder method.
2fill in blank
medium

Complete the code to select only the 'name' and 'email' columns from the 'users' table.

Laravel
$users = DB::table('users')->[1](['name', 'email'])->get();
Drag options to blanks, or click blank then click option'
Aselect
Bpluck
Ccolumns
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using get() with columns array which causes error.
Using pluck() when multiple columns are needed.
3fill in blank
hard

Fix the error in the code to get the first user with id 5.

Laravel
$user = DB::table('users')->where('id', [1])->first();
Drag options to blanks, or click blank then click option'
A'5'
Bid
C"5"
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Passing '5' as a string instead of integer 5.
Using the column name 'id' as value.
4fill in blank
hard

Fill both blanks to select users where 'status' is 'active' and order by 'created_at' descending.

Laravel
$users = DB::table('users')->where('status', [1])->orderBy([2], 'desc')->get();
Drag options to blanks, or click blank then click option'
A'active'
B'created_at'
C'status'
D'desc'
Attempts:
3 left
💡 Hint
Common Mistakes
Using column names instead of values in where clause.
Putting 'desc' in place of column name in orderBy.
5fill in blank
hard

Fill all three blanks to select 'name' and 'email' of users older than 18, ordered by 'name' ascending.

Laravel
$users = DB::table('users')->select([1], [2])->where('age', '>', [3])->orderBy('name', 'asc')->get();
Drag options to blanks, or click blank then click option'
A'name'
B'email'
C18
D'age'
Attempts:
3 left
💡 Hint
Common Mistakes
Using column name 'age' as a value in where clause.
Forgetting quotes around column names in select.