Bird
Raised Fist0
Laravelframework~20 mins

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

Choose your learning style10 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).

Practice

(1/5)
1. What does the orWhere method do in a Laravel query builder?
easy
A. Replaces all previous conditions with a new WHERE clause
B. Adds an OR condition to the existing query conditions
C. Deletes the last WHERE condition from the query
D. Executes the query immediately and returns results

Solution

  1. Step 1: Understand the purpose of orWhere

    The orWhere method adds an OR condition to the existing query, allowing multiple conditions to be combined flexibly.
  2. Step 2: Compare with other methods

    Unlike where which adds AND conditions, orWhere adds OR conditions without replacing or deleting previous ones.
  3. Final Answer:

    Adds an OR condition to the existing query conditions -> Option B
  4. Quick Check:

    orWhere = OR condition [OK]
Hint: orWhere adds OR, not AND, to query conditions [OK]
Common Mistakes:
  • Thinking orWhere replaces previous conditions
  • Confusing orWhere with immediate query execution
  • Assuming orWhere deletes conditions
2. Which of the following is the correct syntax to add an OR condition with a closure in Laravel's query builder?
easy
A. $query->orWhere[function($q) { $q->where('age', '>', 18); }];
B. $query->orWhere(function $q { $q->where('age', '>', 18); });
C. $query->orWhere->function($q) { $q->where('age', '>', 18); };
D. $query->orWhere(function($q) { $q->where('age', '>', 18); });

Solution

  1. Step 1: Identify correct closure syntax

    Laravel expects a closure passed as an argument to orWhere using parentheses and the function($q) syntax.
  2. Step 2: Check for syntax errors in options

    Options A, C, and D misuse brackets, arrows, or omit parentheses, causing syntax errors.
  3. Final Answer:

    $query->orWhere(function($q) { $q->where('age', '>', 18); }); -> Option D
  4. Quick Check:

    Closure syntax uses parentheses and function keyword [OK]
Hint: Use parentheses and function keyword for closures [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Omitting parentheses around closure
  • Incorrect arrow usage with closures
3. Given the following Laravel query builder code, what SQL WHERE clause will it generate?
$query->where('status', 'active')
      ->orWhere(function($q) {
          $q->where('age', '>', 18)
            ->where('role', 'admin');
      });
medium
A. WHERE status = 'active' OR (age > 18 AND role = 'admin')
B. WHERE (status = 'active' OR age > 18) AND role = 'admin'
C. WHERE status = 'active' AND age > 18 OR role = 'admin'
D. WHERE status = 'active' OR age > 18 OR role = 'admin'

Solution

  1. Step 1: Analyze the where and orWhere structure

    The first condition is status = 'active'. Then orWhere adds a grouped condition with two ANDed clauses: age > 18 and role = 'admin'.
  2. Step 2: Translate to SQL

    The SQL will be: WHERE status = 'active' OR (age > 18 AND role = 'admin'), grouping the two conditions inside parentheses after OR.
  3. Final Answer:

    WHERE status = 'active' OR (age > 18 AND role = 'admin') -> Option A
  4. Quick Check:

    orWhere with closure groups AND conditions inside parentheses [OK]
Hint: orWhere with closure groups conditions inside parentheses [OK]
Common Mistakes:
  • Ignoring parentheses grouping in SQL
  • Mixing AND and OR precedence incorrectly
  • Assuming orWhere applies to each condition separately
4. Identify the error in this Laravel query builder code snippet:
$query->where('type', 'user')
      ->orWhere(function($q) {
          $q->where('age', '>', 18)
          ->orWhere('role', 'admin');
      });
medium
A. Using orWhere inside closure causes incorrect grouping
B. Missing semicolon after where inside closure
C. No error, code is correct
D. orWhere cannot be used inside closures

Solution

  1. Step 1: Check for syntax errors

    There are no syntax errors; all method calls and chaining are valid PHP and Laravel syntax. No missing semicolons or invalid operators.
  2. Step 2: Verify query logic

    The code generates SQL: WHERE `type` = 'user' OR (`age` > 18 OR `role` = 'admin'), which matches the chained where followed by orWhere inside the closure.
  3. Final Answer:

    No error, code is correct -> Option C
  4. Quick Check:

    where()->orWhere() inside closure produces OR logic [OK]
Hint: orWhere inside closures is valid and adds OR conditions [OK]
Common Mistakes:
  • Thinking orWhere cannot be used inside closures
  • Believing a semicolon is missing after where (not needed in chains)
  • Assuming mixing where and orWhere causes syntax errors
5. You want to find users who are either active admins older than 30 or guests with a verified email. Which Laravel query correctly uses orWhere and closures to build this condition?
// Users who are (active AND admin AND age > 30) OR (guest AND email_verified = true)
$query->where(function($q) {
    $q->where('status', 'active')
      ->where('role', 'admin')
      ->where('age', '>', 30);
})
->orWhere(function($q) {
    $q->where('status', 'guest')
      ->where('email_verified', true);
});
hard
A. Correctly groups conditions with where and orWhere closures
B. Should use orWhere inside first closure instead of where
C. Needs to chain all conditions with orWhere only
D. Cannot use closures with orWhere for complex conditions

Solution

  1. Step 1: Analyze grouping logic

    The query groups the first set of conditions with where inside a closure, then uses orWhere with another closure for the second group, matching the OR logic between groups.
  2. Step 2: Confirm correct use of closures and orWhere

    This pattern correctly builds SQL like: (active AND admin AND age > 30) OR (guest AND email_verified = true), which matches the requirement.
  3. Final Answer:

    Correctly groups conditions with where and orWhere closures -> Option A
  4. Quick Check:

    Use closures with where and orWhere to group complex conditions [OK]
Hint: Group AND conditions in closures, combine with orWhere [OK]
Common Mistakes:
  • Using orWhere inside closures incorrectly
  • Chaining all conditions with orWhere causing wrong logic
  • Thinking closures can't be used with orWhere
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard