Discover how a tiny method can save you hours of repetitive query writing!
Why Query scopes in Laravel? - Purpose & Use Cases
Imagine writing the same complex database query conditions over and over in different parts of your Laravel app.
For example, filtering users who are active or have a certain role in many places.
Manually repeating query conditions leads to copy-paste errors, inconsistent filters, and hard-to-maintain code.
It becomes a headache to update the logic everywhere if requirements change.
Query scopes let you define reusable query logic inside your model once.
Then you just call a simple method name to apply that filter anywhere.
This keeps your code clean, consistent, and easy to update.
User::where('active', 1)->where('role', 'admin')->get(); User::where('active', 1)->where('role', 'admin')->count();
User::activeAdmin()->get(); User::activeAdmin()->count();
You can write clear, reusable, and maintainable database queries that save time and reduce bugs.
In a team project, everyone can use the same activeAdmin scope to get active admin users without rewriting the conditions.
If the definition of 'active' changes, you update it once in the scope, and the whole app benefits.
Query scopes avoid repeating query logic.
They make your code cleaner and easier to maintain.
Updating query rules becomes simple and centralized.
Practice
Solution
Step 1: Understand query scopes role
Query scopes allow you to write common query parts once and reuse them easily.Step 2: Compare with other options
Defining relationships, migrations, or authentication are unrelated to query scopes.Final Answer:
To reuse common query filters easily across different queries -> Option DQuick Check:
Query scopes = reusable query filters [OK]
- Confusing scopes with relationships
- Thinking scopes create migrations
- Mixing scopes with authentication logic
active in a Laravel model?Solution
Step 1: Recall scope method naming
Query scopes must be prefixed with 'scope' and accept $query as first parameter.Step 2: Check method signature and body
public function scopeActive($query) { return $query->where('status', 'active'); } correctly uses 'scopeActive($query)' and returns $query with condition.Final Answer:
public function scopeActive($query) { return $query->where('status', 'active'); } -> Option AQuick Check:
Scope method = scope + $query param [OK]
- Omitting $query parameter
- Using underscore in method name
- Not prefixing method with 'scope'
public function scopePopular($query) {
return $query->where('votes', '>', 100);
}What will
User::popular()->get() return?Solution
Step 1: Understand the scope condition
The scope filters users where 'votes' is greater than 100.Step 2: Analyze the query call
Calling User::popular()->get() applies the scope and returns matching users.Final Answer:
All users with votes greater than 100 -> Option CQuick Check:
Scope filters votes > 100 = users returned [OK]
- Confusing greater than with less than
- Assuming scope returns all records
- Thinking scope causes syntax error
public function scopeRecent() {
return $this->orderBy('created_at', 'desc');
}Solution
Step 1: Check scope method signature
Scope methods must accept $query as the first parameter to modify the query.Step 2: Identify missing parameter
The method lacks the $query parameter, so it cannot modify the query builder.Final Answer:
Missing $query parameter in the scope method -> Option AQuick Check:
Scope method needs $query param [OK]
- Forgetting $query parameter
- Thinking orderBy is disallowed in scopes
- Confusing method naming rules
activeAndRecent that returns users who are active and created within the last 30 days. Which of the following scope definitions is correct?Solution
Step 1: Verify method signature and parameters
The scope method must be named with 'scope' prefix and accept $query parameter.Step 2: Check query conditions logic
Use 'where' twice to combine conditions with AND logic for active status and recent date.Step 3: Compare options
public function scopeActiveAndRecent($query) { return $query->where('status', 'active') ->where('created_at', '>=', now()->subDays(30)); } correctly uses 'scopeActiveAndRecent($query)' and combines conditions with 'where'.Final Answer:
public function scopeActiveAndRecent($query) { return $query->where('status', 'active') ->where('created_at', '>=', now()->subDays(30)); } -> Option BQuick Check:
Scope with $query and AND conditions = public function scopeActiveAndRecent($query) { return $query->where('status', 'active') ->where('created_at', '>=', now()->subDays(30)); } [OK]
- Using orWhere instead of where for AND logic
- Omitting $query parameter
- Not prefixing method with 'scope'
Ready to prove it?
Pass the test and this pattern moves unknown
