0
0
Laravelframework~10 mins

Query scopes in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Query scopes
Define scope method in model
Call scope method on query builder
Scope modifies query
Execute query with modified conditions
Get filtered results
Query scopes let you add reusable filters to database queries by defining methods in your model that modify the query builder.
Execution Sample
Laravel
class Post extends Model {
  public function scopePublished($query) {
    return $query->where('published', true);
  }
}

$posts = Post::published()->get();
Defines a scope 'published' to filter posts where 'published' is true, then fetches those posts.
Execution Table
StepActionQuery Builder StateResult
1Call Post::published()Query builder for Post modelQuery builder instance created
2Inside scopePublished: add where('published', true)Query builder with where condition addedQuery modified to filter published posts
3Call get() on query builderFinal query readySQL executed, returns posts with published = true
4Receive resultsCollection of Post modelsFiltered posts returned
💡 Query executed after scope modifies it, returning only published posts
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$querynullQuery builder instanceQuery builder with where('published', true)Query builder ready to executeExecuted query results
$postsnullnullnullnullCollection of published posts
Key Moments - 3 Insights
Why does the scope method start with 'scope' in its name?
Laravel requires scope methods to start with 'scope' so it can recognize and call them properly when you use the short form like 'published()' in the query (see execution_table step 1 and 2).
What does the scope method receive as its first argument?
The scope method receives the query builder instance as the first argument, allowing it to add conditions to the query (see execution_table step 2 where the where condition is added).
Can you chain multiple scopes together?
Yes, because each scope returns the modified query builder, you can chain scopes like Post::published()->popular()->get(), building up the query step by step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does the query builder contain after step 2?
AA query with a where condition filtering published posts
BAn empty query builder with no conditions
CA collection of posts already fetched
DA raw SQL string
💡 Hint
Check the 'Query Builder State' column at step 2 in execution_table
At which step is the SQL query actually executed?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step where 'get()' is called in execution_table
If the scope method did not return the query builder, what would happen?
AThe query would still work fine
BThe query builder would be lost and chaining scopes would fail
CThe query would return all posts ignoring filters
DLaravel would throw a syntax error
💡 Hint
Refer to variable_tracker for how $query changes and is returned after step 2
Concept Snapshot
Query scopes in Laravel are methods in models prefixed with 'scope'.
They receive the query builder and add conditions.
Call them as short names on the model query.
Scopes return the modified query builder for chaining.
They help reuse query filters cleanly.
Full Transcript
Query scopes in Laravel let you add reusable filters to database queries by defining methods in your model. These methods start with 'scope' and receive the query builder as the first argument. Inside the scope, you add conditions like where clauses. When you call the scope on the model, Laravel calls the method without the 'scope' prefix and applies the filter. The scope returns the modified query builder so you can chain multiple scopes. Finally, calling get() executes the SQL query and returns filtered results. This visual trace showed how the query builder changes step-by-step from creation, modification by the scope, to execution and result retrieval.