A query scope is a way to define common query logic inside a model. It lets you reuse query conditions easily by calling a method on the model's query builder.
Click to reveal answer
beginner
How do you define a local query scope in a Laravel model?
Define a method in the model starting with scope followed by the scope name. For example, scopeActive($query) defines a scope called active. The method receives the query builder as the first argument.
Click to reveal answer
beginner
How do you use a local query scope in Laravel when querying?
Call the scope method name without the scope prefix on the model query. For example, User::active()->get() uses the scopeActive method.
Click to reveal answer
intermediate
What is a global query scope in Laravel?
A global query scope automatically adds conditions to all queries for a model. It is useful for things like soft deletes or multi-tenant filtering.
Click to reveal answer
intermediate
How do you remove a global scope from a Laravel query?
Use the <code>withoutGlobalScope()</code> method on the query builder and pass the scope class or name to exclude it from the query.
Click to reveal answer
How do you name a local query scope method in a Laravel model?
AStart with 'scope' followed by the scope name
BStart with 'query' followed by the scope name
CUse any method name without prefix
DStart with 'filter' followed by the scope name
✗ Incorrect
Local query scopes must start with 'scope' followed by the scope name, like 'scopeActive'.
How do you apply a local query scope named 'active' when querying a model?
AUser::scopeActive()->get()
BUser::active()->get()
CUser::filterActive()->get()
DUser::whereActive()->get()
✗ Incorrect
You call the scope without the 'scope' prefix: User::active()->get().
What is the main purpose of a global query scope?
ATo handle database migrations
BTo define reusable query methods manually called
CTo create database indexes
DTo add conditions to all queries automatically
✗ Incorrect
Global scopes add conditions automatically to every query on the model.
How can you exclude a global scope from a query?
AUse withoutGlobalScope() method
BUse removeScope() method
CUse ignoreScope() method
DUse skipScope() method
✗ Incorrect
The method withoutGlobalScope() removes a global scope from the query.
Which argument does a local query scope method receive first?
AThe model instance
BThe database connection
CThe query builder
DThe request object
✗ Incorrect
The first argument is the query builder to modify the query.
Explain how to create and use a local query scope in Laravel.
Think about naming and how you call the scope when querying.
You got /3 concepts.
Describe what a global query scope is and how to remove it from a query.
Consider automatic query changes and how to opt out.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using query scopes in a Laravel model?
easy
A. To create database migrations
B. To define database table relationships
C. To handle user authentication
D. To reuse common query filters easily across different queries
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 D
Quick Check:
Query scopes = reusable query filters [OK]
Hint: Query scopes = reusable query parts in models [OK]
Common Mistakes:
Confusing scopes with relationships
Thinking scopes create migrations
Mixing scopes with authentication logic
2. Which of the following is the correct way to define a query scope named active in a Laravel model?
easy
A. public function scopeActive($query) { return $query->where('status', 'active'); }
B. public function scopeActive() { return $this->where('status', 'active'); }
C. public function scope_active($query) { return $query->where('status', 'active'); }
D. public function active() { return $this->where('status', 'active'); }
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 A
Quick Check:
Scope method = scope + $query param [OK]
Hint: Scope methods start with 'scope' and take $query param [OK]
Common Mistakes:
Omitting $query parameter
Using underscore in method name
Not prefixing method with 'scope'
3. Given this scope in a Laravel model:
public function scopePopular($query) {
return $query->where('votes', '>', 100);
}
What will User::popular()->get() return?
medium
A. All users regardless of votes
B. All users with votes less than or equal to 100
C. All users with votes greater than 100
D. Syntax error because scope is missing
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.
public function scopeRecent() {
return $this->orderBy('created_at', 'desc');
}
medium
A. Missing $query parameter in the scope method
B. Incorrect method name, should not start with 'scope'
C. Using orderBy inside scope is not allowed
D. Missing return type declaration
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 A
Quick Check:
Scope method needs $query param [OK]
Hint: Scope methods always need $query parameter [OK]
Common Mistakes:
Forgetting $query parameter
Thinking orderBy is disallowed in scopes
Confusing method naming rules
5. You want to create a reusable scope activeAndRecent that returns users who are active and created within the last 30 days. Which of the following scope definitions is correct?
hard
A. public function scopeActiveAndRecent($query) {
return $query->where('status', 'active')
->orWhere('created_at', '>=', now()->subDays(30));
}
B. public function scopeActiveAndRecent($query) {
return $query->where('status', 'active')
->where('created_at', '>=', now()->subDays(30));
}
C. public function scopeActiveAndRecent() {
return $this->where('status', 'active')
->where('created_at', '>=', now()->subDays(30));
}
D. public function activeAndRecent($query) {
return $query->where('status', 'active')
->where('created_at', '>=', now()->subDays(30));
}
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 B
Quick Check:
Scope with $query and AND conditions = public function scopeActiveAndRecent($query) {
return $query->where('status', 'active')
->where('created_at', '>=', now()->subDays(30));
} [OK]
Hint: Use 'where' twice for AND, scope method needs $query [OK]
Common Mistakes:
Using orWhere instead of where for AND logic
Omitting $query parameter
Not prefixing method with 'scope'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.