Chaining multiple where clauses applies AND logic between conditions.
Step 2: Analyze the conditions
It filters products where price is greater than 100 AND stock is at least 10.
Final Answer:
All products with price > 100 and stock >= 10 -> Option A
Quick Check:
Multiple where = AND filter [OK]
Hint: Multiple where clauses combine with AND logic [OK]
Common Mistakes:
Assuming where clauses combine with OR by default
Thinking multiple where cause syntax error
Confusing comparison operators
4. Identify the error in this Laravel query:
User::where('status', 'active')->get();
medium
A. Missing semicolon at the end
B. Incorrect method name, should be filter
C. No error, query is correct
D. Missing operator in where clause
Solution
Step 1: Check where clause parameters
The where method accepts two parameters: column and value (assumes =), or three: column, operator, value.
Step 2: Analyze the query
User::where('status', 'active')->get(); uses two parameters correctly, assuming status = 'active'.
Final Answer:
No error, query is correct -> Option C
Quick Check:
where needs column and value or operator [OK]
Hint: where needs column and value or operator [OK]
Common Mistakes:
Thinking where always requires explicit operator
Confusing method names like filter instead of where
Ignoring Laravel's default operator '=' behavior
5. How would you write a Laravel query to get all orders where the status is 'completed' and the total is greater than 500, but only if the user is an admin? Assume you have a variable $isAdmin that is true or false.
hard
A. Order::when($isAdmin, fn($q) => $q->where('status', 'completed')->where('total', '>', 500))->get();
B. Order::where('status', 'completed')->where('total', '>', 500)->get();
C. Order::if($isAdmin)->where('status', 'completed')->where('total', '>', 500)->get();
D. Order::where('status', 'completed' && $isAdmin)->where('total', '>', 500)->get();
Solution
Step 1: Understand conditional queries in Laravel
Laravel's when method allows adding conditions only if a boolean is true.
Step 2: Analyze each option
Order::when($isAdmin, fn($q) => $q->where('status', 'completed')->where('total', '>', 500))->get(); uses when correctly to add where clauses only if $isAdmin is true. Order::where('status', 'completed')->where('total', '>', 500)->get(); always applies filters. Order::if($isAdmin)->where('status', 'completed')->where('total', '>', 500)->get(); uses invalid if method. Order::where('status', 'completed' && $isAdmin)->where('total', '>', 500)->get(); has wrong syntax mixing boolean and string.
Final Answer:
Order::when($isAdmin, fn($q) => $q->where('status', 'completed')->where('total', '>', 500))->get(); -> Option A
Quick Check:
Use when() for conditional where clauses [OK]
Hint: Use when() to add where only if condition true [OK]
Common Mistakes:
Applying where clauses unconditionally
Using non-existent if() method on query builder
Incorrect syntax mixing booleans inside where
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.