Laravel

Laravel Scopes: Reusable Query Logic

December 24, 2025 1 min read 32 views

Use scopes for reusable queries:

Local Scopes

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

Using Scopes

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

Dynamic Scopes

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

Global Scopes

class ActiveScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        $builder->where('active', 1);
    }
}
Share this post:

Related Posts

Comments (0)

Please log in to leave a comment. Log in

No comments yet. Be the first to comment!