Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Laravel

Laravel Scopes: Reusable Query Logic

Write cleaner queries with local and global scopes in Laravel.

24 December 20251 min readLaravelاقرأ بالعربية

On this page

  1. Local Scopes
  2. Using Scopes
  3. Dynamic Scopes
  4. Global Scopes

Use scopes for reusable queries:

Local Scopes

php
public function scopePopular($query)
{
    return $query->where('views', '>', 1000);
}

Using Scopes

php
$posts = Post::popular()->published()->get();

Dynamic Scopes

php
public function scopeOfType($query, $type)
{
    return $query->where('type', $type);
}

Global Scopes

php
class ActiveScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        $builder->where('active', 1);
    }
}
All articles

Keep reading

  • Laravel Action Classes Pattern16 January 20261 min read
  • Laravel Action Classes: Single Responsibility16 January 20261 min read
  • Laravel API Rate Limiting16 January 20261 min read