0
0
Laravelframework~20 mins

Raw expressions in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Raw Expression Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of raw expression in a query builder
What will be the output SQL query generated by this Laravel query builder code?
Laravel
$query = DB::table('users')
    ->select(DB::raw('count(*) as user_count'))
    ->toSql();
echo $query;
Aselect count(*) as user_count from users
Bselect count(*) as user_count from `users`
Cselect count(*) user_count from `users`
Dselect count(*) from `users`
Attempts:
2 left
💡 Hint
Remember Laravel adds backticks around table names in the generated SQL.
component_behavior
intermediate
2:00remaining
Effect of raw expressions on query bindings
Given this Laravel query, what will be the value of the bindings array after execution?
Laravel
$query = DB::table('orders')
    ->whereRaw('price > ?', [100])
    ->where('status', '=', 'paid');
$bindings = $query->getBindings();
A[100, 'paid']
B['paid', 100]
C[100]
D['paid']
Attempts:
2 left
💡 Hint
Bindings are collected in the order they appear in the query.
🔧 Debug
advanced
2:00remaining
Why does this raw expression cause a syntax error?
Consider this Laravel query: DB::table('products') ->select(DB::raw('SUM(price) AS total_price')) ->whereRaw('category = ?', 'electronics') ->get(); Why does this code cause an error?
AThe DB::raw expression is missing parentheses around SUM(price)
BThe table name 'products' must be wrapped in backticks in DB::table
CThe select method cannot accept raw expressions
DThe whereRaw method expects the second argument to be an array, not a string
Attempts:
2 left
💡 Hint
Check the expected argument types for whereRaw bindings.
📝 Syntax
advanced
2:00remaining
Correct syntax for raw order by clause
Which option correctly orders users by a raw expression that calculates length of the name?
Laravel
DB::table('users')->orderByRaw(???)->get();
A'LENGTH(name) DESC'
BLENGTH(name) DESC
C['LENGTH(name) DESC']
DDB::raw('LENGTH(name) DESC')
Attempts:
2 left
💡 Hint
orderByRaw expects a string with the raw SQL expression.
🧠 Conceptual
expert
3:00remaining
Understanding security risks with raw expressions
Which statement best explains the security risk when using raw expressions in Laravel queries?
AUsing raw expressions disables all query logging in Laravel
BRaw expressions automatically sanitize all inputs to prevent SQL injection
CRaw expressions can introduce SQL injection if user input is concatenated directly without bindings
DRaw expressions prevent Laravel from caching query results
Attempts:
2 left
💡 Hint
Think about how raw SQL is handled compared to parameter binding.