Recall & Review
beginner
What is a raw expression in Laravel's query builder?
A raw expression lets you write plain SQL inside Laravel's query builder. It tells Laravel not to escape or modify the SQL part you write.
Click to reveal answer
beginner
How do you create a raw expression in Laravel?
Use the DB::raw() method. For example: DB::raw('COUNT(*)') creates a raw SQL expression for counting rows.
Click to reveal answer
intermediate
Why should you be careful when using raw expressions?
Raw expressions bypass Laravel's protection against SQL injection. You must ensure the raw SQL is safe and does not include user input directly.
Click to reveal answer
beginner
Example: How to select users with a raw SQL condition for age > 30?
Use: User::whereRaw('age > 30')->get(); This runs the raw SQL condition inside the query.
Click to reveal answer
intermediate
Can you use raw expressions in select statements?
Yes! For example: User::select(DB::raw('COUNT(*) as user_count'))->get(); This counts users using raw SQL inside select.
Click to reveal answer
What does DB::raw() do in Laravel?
✗ Incorrect
DB::raw() lets you write raw SQL expressions that Laravel will not modify or escape.
Which method uses raw SQL conditions in Laravel queries?
✗ Incorrect
whereRaw() allows you to add raw SQL conditions to your query.
Why is it risky to use raw expressions with user input?
✗ Incorrect
Raw expressions bypass Laravel's protections, so unsafe user input can lead to SQL injection.
How do you count rows using raw expressions in Laravel?
✗ Incorrect
You use select(DB::raw('COUNT(*) as count')) to count rows with raw SQL.
Which of these is NOT a use of raw expressions?
✗ Incorrect
Raw expressions do NOT automatically escape user input; they bypass protections.
Explain what raw expressions are in Laravel and when you might use them.
Think about how Laravel lets you write plain SQL inside its query builder.
You got /4 concepts.
Describe how to safely use raw expressions in Laravel queries.
Focus on security best practices when mixing raw SQL with user data.
You got /4 concepts.