0
0
Laravelframework~20 mins

OrWhere and advanced conditions in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel OrWhere Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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();
Aselect * from `users` where `status` = ? or `role` = ?
Bselect * from `users` where `status` = ? and `role` = ?
Cselect * from `users` where `status` = ? or `role` != ?
Dselect * from `users` where `status` != ? or `role` = ?
Attempts:
2 left
💡 Hint
Remember that orWhere adds an OR condition to the previous WHERE clause.
📝 Syntax
intermediate
2: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?
A
User::where('status', 'active')
    ->orWhere('role', 'admin')
    ->where('verified', true)
    ->get();
B
User::where('status', 'active')
    ->orWhere(function($query) {
        $query->where('role', 'admin')
        ->orWhere('verified', true);
    })
    ->get();
C
User::where('status', 'active')
    ->where(function($query) {
        $query->orWhere('role', 'admin')
              ->where('verified', true);
    })
    ->get();
D
User::where('status', 'active')
    ->orWhere(function($query) {
        $query->where('role', 'admin')
              ->where('verified', true);
    })
    ->get();
Attempts:
2 left
💡 Hint
Use a closure inside orWhere to group multiple conditions combined with AND.
🔧 Debug
advanced
2: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();
ABecause the where('verified', true) applies only to the last orWhere condition, not grouped properly.
BBecause orWhere cannot be used after where; it must be first.
CBecause the query is missing a join to the roles table.
DBecause the where('status', 'active') is ignored when orWhere is used.
Attempts:
2 left
💡 Hint
Think about how Laravel groups where and orWhere conditions without closures.
🧠 Conceptual
advanced
2: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?
AWHERE `status` = ? OR (`role` = ? AND `verified` = ?)
BWHERE (`status` = ? OR `role` = ?) AND `verified` = ?
CWHERE `status` = ? OR `role` = ? OR `verified` = ?
DWHERE `status` = ? AND `role` = ? AND `verified` = ?
Attempts:
2 left
💡 Hint
Closures group conditions inside parentheses in SQL.
state_output
expert
2: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();
A5
B2
C4
D3
Attempts:
2 left
💡 Hint
Check which rows satisfy status = active OR (role = admin AND verified = true).