Eloquent Scopes are one of Laravel’s most elegant features for organizing database queries and keeping your codebase clean.
Instead of repeating query conditions across controllers and services, scopes allow you to define reusable constraints directly inside the model.
## What Are Eloquent Scopes?
Eloquent Scopes are reusable query methods defined inside your Eloquent models.
Laravel supports two types:
- Local Scopes
- Global Scopes
## Local Scopes
Local scopes are perfect for repeated query conditions.
### Example
```php
class Post extends Model
{
public function scopePublished($query)
{
return $query->where('published', true);
}
public function scopeLatestPublished($query)
{
return $query->published()
->orderByDesc('published_at');
}
}
Usage
Post::published()->get();
Post::latestPublished()->paginate(10);
The result:
Cleaner queries
Better readability
Less duplication
Why Scopes Matter
Without scopes, query logic often spreads across:
Controllers
Services
Repositories
Scopes centralize query logic inside the model, improving:
Maintainability
Readability
Long-term scalability
Global Scopes
Global scopes apply constraints automatically to all queries.
Common Use Cases
Soft deletes
Multi-tenant filtering
Active-only records
class ActiveScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->where('is_active', true);
}
}
Best Practices
Use clear and meaningful scope names
Keep scopes focused on query logic
Combine scopes for flexible queries
Prefer scopes over duplicated conditions
Final Thoughts
Eloquent Scopes don’t just shorten queries —
they significantly improve the structure of your Laravel applications.
A must-use feature for every Laravel developer 💜
Laravel Eloquent Scopes: Cleaner & Reusable Queries
Key Takeaways
- Laravel Eloquent Scopes help you write cleaner, reusable, and expressive database queries by encapsulating query logic directly inside your models.
Added a complete explanation of Eloquent Scopes with real-world examples.
Mohamed Shahin
Software Engineer
You might also like
Implementing Feature Flags in Laravel with Laravel Toggle
Understanding Feature Flags in Laravel Feature flags, also known as feature toggles, are powerful techniques in modern software development that allow you to tu...
Implementing Simple Feature Flags in Laravel with Laravel Toggle
Feature flags, also known as feature toggles, are a powerful technique in modern software development. They allow you to turn features of your application on or...
Piper: Unlocking Laravel's Array and String Power in Pure PHP with the Pipe Operator
Laravel's fluent collection and string manipulation methods are a joy to work with, offering concise and readable ways to transform data. What if you could brin...
Mastering Laravel MongoDB Full-Text Search: The Art of Relevancy with BM25 and Field Weighting
Introduction to Full-Text Search and MongoDB Atlas Search In today's data-rich applications, a powerful and accurate search feature is paramount. Users expect f...