Bird
Raised Fist0
Laravelframework~10 mins

Where clauses in Laravel - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a simple where clause filtering users by 'active' status.

Laravel
$users = DB::table('users')->where('status', [1])->get();
Drag options to blanks, or click blank then click option'
A'active'
B'inactive'
C'pending'
D'deleted'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the status value.
Using a wrong status string.
2fill in blank
medium

Complete the code to filter users whose age is greater than 18 using a where clause.

Laravel
$users = DB::table('users')->where('age', [1], 18)->get();
Drag options to blanks, or click blank then click option'
A=
B<
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '>' causes filtering only age exactly 18.
Using '<' or '<=' filters younger users.
3fill in blank
hard

Fix the error in the where clause to filter users with email ending in '@example.com'.

Laravel
$users = DB::table('users')->where('email', [1], '%@example.com')->get();
Drag options to blanks, or click blank then click option'
ALIKE
B=
CIN
DBETWEEN
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' does not support wildcards and will not match patterns.
Using 'IN' or 'BETWEEN' is not valid for this pattern.
4fill in blank
hard

Fill both blanks to filter users who are active and have age less than 30.

Laravel
$users = DB::table('users')->where('status', [1], 'active')->where('age', [2], 30)->get();
Drag options to blanks, or click blank then click option'
A=
B>
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators like '>' for age.
Using '!=' instead of '=' for status.
5fill in blank
hard

Fill all three blanks to filter users with name starting with 'J', age greater or equal to 21, and status not equal to 'inactive'.

Laravel
$users = DB::table('users')->where('name', [1], 'J%')->where('age', [2], 21)->where('status', [3], 'inactive')->get();
Drag options to blanks, or click blank then click option'
ALIKE
B>=
C!=
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '!=' for status.
Using '<' instead of '>=' for age.
Using '=' instead of 'LIKE' for name pattern.

Practice

(1/5)
1. What does the where clause do in a Laravel query?
easy
A. It sorts records in ascending order.
B. It filters records based on a condition.
C. It deletes records from the database.
D. It joins two tables together.

Solution

  1. Step 1: Understand the purpose of where clause

    The where clause is used to filter database records by specifying conditions.
  2. Step 2: Compare with other options

    Sorting, deleting, and joining are different operations not done by where.
  3. Final Answer:

    It filters records based on a condition. -> Option B
  4. Quick Check:

    Where clause = filter condition [OK]
Hint: Where means filter records by condition [OK]
Common Mistakes:
  • Confusing where with orderBy for sorting
  • Thinking where deletes records
  • Mixing where with join operations
2. Which of the following is the correct syntax to get users with age 25 using Laravel's query builder?
easy
A. User::where('age' => 25)->get();
B. User::where('age' == 25)->get();
C. User::where(age, 25)->get();
D. User::where('age', '=', 25)->get();

Solution

  1. Step 1: Recall correct where syntax

    The correct syntax uses three parameters: column, operator, value. Operator '=' is optional but valid.
  2. Step 2: Check each option

    User::where('age', '=', 25)->get(); uses correct syntax. User::where('age' == 25)->get(); uses '==' inside where which is invalid. User::where(age, 25)->get(); uses unquoted column name causing undefined constant. User::where('age' => 25)->get(); uses '=>' which is wrong.
  3. Final Answer:

    User::where('age', '=', 25)->get(); -> Option D
  4. Quick Check:

    Correct where syntax = User::where('age', '=', 25)->get(); [OK]
Hint: Use where('column', '=', value) for filtering [OK]
Common Mistakes:
  • Using '==' instead of ',' in where
  • Forgetting quotes around column name
  • Using '=>' instead of ',' in parameters
3. What will be the result of this Laravel query?
Product::where('price', '>', 100)->where('stock', '>=', 10)->get();
medium
A. All products with price > 100 and stock >= 10
B. All products with price > 100 or stock >= 10
C. All products with price <= 100 and stock < 10
D. Syntax error due to multiple where clauses

Solution

  1. Step 1: Understand chaining of where clauses

    Chaining multiple where clauses applies AND logic between conditions.
  2. Step 2: Analyze the conditions

    It filters products where price is greater than 100 AND stock is at least 10.
  3. Final Answer:

    All products with price > 100 and stock >= 10 -> Option A
  4. 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

  1. Step 1: Check where clause parameters

    The where method accepts two parameters: column and value (assumes =), or three: column, operator, value.
  2. Step 2: Analyze the query

    User::where('status', 'active')->get(); uses two parameters correctly, assuming status = 'active'.
  3. Final Answer:

    No error, query is correct -> Option C
  4. 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

  1. Step 1: Understand conditional queries in Laravel

    Laravel's when method allows adding conditions only if a boolean is true.
  2. 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.
  3. Final Answer:

    Order::when($isAdmin, fn($q) => $q->where('status', 'completed')->where('total', '>', 500))->get(); -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard