Laravel Scopes: Reusable Query Logic
December 24, 2025
•
1 min read
•
32 views
Table of Contents
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);
}
}
Related Posts
Laravel Sanctum API Authentication Complete Guide
Build secure API authentication with Laravel Sanctum for SPAs and mobile apps.
Laravel Rate Limiting: Protect Your Application
Implement rate limiting to protect your Laravel application from abuse.
Laravel Blade Components: Build Reusable UI
Create powerful reusable components with Laravel Blade.
Comments (0)
No comments yet. Be the first to comment!