0
0
Laravelframework~10 mins

Raw expressions 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 users with a raw SQL expression.

Laravel
DB::table('users')->select(DB::raw([1]))->get();
Drag options to blanks, or click blank then click option'
A"*"
B"users"
C"count(*) as user_count"
D"id"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to wrap the SQL expression in quotes.
Using column names directly without DB::raw.
2fill in blank
medium

Complete the code to order users by a raw SQL expression.

Laravel
DB::table('users')->orderByRaw([1])->get();
Drag options to blanks, or click blank then click option'
A"name DESC"
B"created_at DESC"
C"age ASC"
D"email ASC"
Attempts:
3 left
💡 Hint
Common Mistakes
Using orderBy instead of orderByRaw for raw SQL.
Missing quotes around the raw SQL string.
3fill in blank
hard

Fix the error in the raw where clause to filter users by age.

Laravel
DB::table('users')->whereRaw([1])->get();
Drag options to blanks, or click blank then click option'
A"age > 18 OR 1=1"
Bage > 18
C"age > '18'"
D"age > 18"
Attempts:
3 left
💡 Hint
Common Mistakes
Writing raw SQL without quotes causes errors.
Adding unsafe SQL like OR 1=1 can cause security issues.
4fill in blank
hard

Fill both blanks to create a raw select with alias and a where condition.

Laravel
DB::table('orders')->select(DB::raw([1]))->whereRaw([2])->get();
Drag options to blanks, or click blank then click option'
A"SUM(price) as total_price"
B"price > 100"
C"status = 'completed'"
D"COUNT(*) as order_count"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up select and where raw expressions.
Forgetting quotes around raw SQL strings.
5fill in blank
hard

Fill all three blanks to build a raw expression with a case statement, alias, and order.

Laravel
DB::table('products')->select(DB::raw([1]))->orderByRaw([2])->whereRaw([3])->get();
Drag options to blanks, or click blank then click option'
A"CASE WHEN stock > 0 THEN 'In Stock' ELSE 'Out of Stock' END as availability"
B"availability DESC"
C"price < 50"
D"stock > 10"
Attempts:
3 left
💡 Hint
Common Mistakes
Not wrapping CASE statement in quotes.
Using invalid SQL syntax inside raw expressions.