Challenge - 5 Problems
Laravel OrWhere Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the resulting SQL query from this Laravel Eloquent snippet?
Consider the following Laravel Eloquent query builder code. What SQL query does it produce?
Laravel
User::where('status', 'active') ->orWhere('role', 'admin') ->toSql();
Attempts:
2 left
💡 Hint
Remember that orWhere adds an OR condition to the previous WHERE clause.
✗ Incorrect
The orWhere method adds an OR condition to the query. So the SQL will have WHERE status = ? OR role = ?.
📝 Syntax
intermediate2:00remaining
Which option correctly uses orWhere with a closure for grouped conditions?
You want to find users who are either active or have the role admin and are verified. Which Laravel query is correct?
Attempts:
2 left
💡 Hint
Use a closure inside orWhere to group multiple conditions combined with AND.
✗ Incorrect
Option D groups role = admin AND verified = true inside orWhere, so the query is: status = active OR (role = admin AND verified = true).
🔧 Debug
advanced2:00remaining
Why does this Laravel query not return expected results?
Given this code, why might the query not return users who are active or admins who are verified?
User::where('status', 'active')
->orWhere('role', 'admin')
->where('verified', true)
->get();
Attempts:
2 left
💡 Hint
Think about how Laravel groups where and orWhere conditions without closures.
✗ Incorrect
The where('verified', true) applies only to the last condition, so the query is: (status = active) OR (role = admin AND verified = true) is NOT guaranteed. Instead, it becomes: (status = active) OR (role = admin) AND verified = true, which changes logic due to operator precedence.
🧠 Conceptual
advanced2:00remaining
What is the effect of this Laravel query on the generated SQL?
Examine this Laravel query:
User::where(function($query) {
$query->where('status', 'active')
->orWhere('role', 'admin');
})->where('verified', true)->toSql();
What does the SQL WHERE clause look like?
Attempts:
2 left
💡 Hint
Closures group conditions inside parentheses in SQL.
✗ Incorrect
The closure groups status and role with OR inside parentheses, then the outer where adds AND verified = true.
❓ state_output
expert2:00remaining
What is the count of users returned by this Laravel query?
Assume the users table has these rows:
| id | status | role | verified |
|----|---------|-------|----------|
| 1 | active | user | true |
| 2 | inactive| admin | true |
| 3 | active | admin | false |
| 4 | inactive| user | true |
| 5 | active | admin | true |
What is the count of users returned by this query?
User::where('status', 'active')
->orWhere(function($query) {
$query->where('role', 'admin')
->where('verified', true);
})
->get()
->count();
Attempts:
2 left
💡 Hint
Check which rows satisfy status = active OR (role = admin AND verified = true).
✗ Incorrect
Rows matching status = active: ids 1,3,5. Rows matching role = admin AND verified = true: ids 2,5. Combined with OR: ids 1,2,3,5. But careful: the query is status = active OR (role = admin AND verified = true). So ids 1,3,5 (active) plus id 2 (admin & verified). So total 4 rows.