Local & Global Query Scopes
Scopes encapsulate common query constraints into reusable methods. Local scopes are called explicitly. Global scopes apply automatically to every query on the model (like SoftDeletes filtering out deleted records).
Query Scopes
class Post extends Model
{
// Local scope — called explicitly
public function scopePublished(Builder $query): Builder
{
return $query->where('published', true)
->whereNotNull('published_at');
}
public function scopeByAuthor(Builder $query, User $user): Builder
{
return $query->where('user_id', $user->id);
}
public function scopeRecent(Builder $query): Builder
{
return $query->orderBy('created_at', 'desc');
}
}
// Usage — clean, chainable calls
$posts = Post::published()->recent()->get();
$posts = Post::published()->byAuthor($user)->paginate(10);
// Global scope — applies to ALL queries
// php artisan make:scope ActiveScope
class ActiveScope implements Scope
{
public function apply(Builder $builder, Model $model): void
{
$builder->where('active', true);
}
}
// In model:
protected static function booted(): void
{
static::addGlobalScope(new ActiveScope);
}
// All Post queries now filter active=true
// To bypass: Post::withoutGlobalScope(ActiveScope::class)->get();Tip
Tip
Practice Local Global Query Scopes in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Reusable query filters.
Practice Task
Note
Practice Task — (1) Write a working example of Local Global Query Scopes from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Local Global Query Scopes is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.