Comparison between Scopes + Traits × UseEloquentBuilder in Laravel
July 13, 2025Laravel provides multiple ways to write reusable query logic. The two most common approaches are using Scopes with Traits or the newer #[UseEloquentBuilder] feature introduced in Laravel 12.19.
🔹 First: Using Scopes with Traits
Scopes are simple methods that begin with scope and live in your Eloquent models. You can group them into Traits to reuse across multiple models.
Example Trait with multiple scopes:
// app/Traits/ArticleScopes.php
namespace App\Traits;
trait ArticleScopes {
public function scopePublished($query) {
return $query->where('is_published', true);
}
public function scopeRecent($query, $days = 7) {
return $query->whereDate('created_at', '>=', now()->subDays($days));
}
public function scopeByAuthor($query, $authorId) {
return $query->where('author_id', $authorId);
}
}
Usage in your model:
// app/Models/Article.php
use App\Traits\ArticleScopes;
class Article extends Model {
use ArticleScopes;
}
And using it in a query:
$articles = Article::published()->recent(30)->byAuthor(5)->paginate(10);
✅ Advantages:
- Quick and easy to implement.
- Reusable across different models.
- Minimal boilerplate.
❌ Disadvantages:
- Query logic can be spread across many traits.
- Limited IDE support for autocomplete or type inference.
- Harder to test scopes in isolation.
🔹 Second: #[UseEloquentBuilder] in Laravel 12.19
With Laravel 12.19, you can now declare a dedicated Builder class for each model using the #[UseEloquentBuilder] attribute, making your query logic more organized and type-safe.
Custom Builder Example:
// app/Builders/ArticleBuilder.php
namespace App\Builders;
use Illuminate\Database\Eloquent\Builder;
class ArticleBuilder extends Builder {
public function published(): static {
return $this->where('is_published', true);
}
public function recent(int $days = 7): static {
return $this->whereDate('created_at', '>=', now()->subDays($days));
}
public function byAuthor(int $authorId): static {
return $this->where('author_id', $authorId);
}
}
Apply it to your model:
// app/Models/Article.php
namespace App\Models;
use App\Builders\ArticleBuilder;
use Illuminate\Database\Eloquent\Attributes\UseEloquentBuilder;
#[UseEloquentBuilder(ArticleBuilder::class)]
class Article extends Model {
// No need to override newEloquentBuilder
}
Query using the custom builder:
$articles = Article::query()->published()->recent(15)->byAuthor(2)->get();
✅ Advantages:
- Centralized query logic – easy to manage.
- Excellent IDE support (autocomplete and type hints).
- Testable as standalone logic.
- More flexible method naming than scopes.
❌ Disadvantages:
- Requires more boilerplate (builder class per model).
- May feel overkill for very simple applications.
📊 Quick Comparison:
| Feature | Scopes + Traits | #[UseEloquentBuilder] |
|---|---|---|
| Ease of Use | ✅ Very simple | 🔸 Requires builder setup |
| Code Organization | 🔸 Medium | ✅ Excellent |
| IDE Support | 🔸 Limited | ✅ Strong |
| Testability | 🔸 Harder | ✅ Easier to isolate |
| Best for Large Projects | 🔸 Can become messy | ✅ Best suited |
🧠 Conclusion:
If your app is small and only needs a few reusable queries, Scopes + Traits are sufficient. But for more advanced or scalable projects, #[UseEloquentBuilder] offers a much cleaner and maintainable approach to query management.
Blog
Explore the Most Powerful Modern Laravel Tools: Inertia.js, View Creators, and HLS — Step by Step
Jul 27, 2025
Here’s a complete breakdown of essential tools to level up your Laravel development: Inertia.js v2, View Creators, and the Laravel HLS package...
What’s New in ECMAScript 2025
Jun 30, 2025
What’s New in ECMAScript 2025 On June 25, 2025, Ecma International officially approved ES2025, adding several useful features: 1. 📦 Import At...
The Difference Between Redux, Context & React Components in State Management
Aug 06, 2025
When building applications with React, there’s always a need to manage data that changes based on user interaction or from fetching external r...
Notifire + FCM with Laravel
Sep 13, 2025
If you want to send Push Notifications from your Laravel app to mobile or web clients, the fastest and simplest way is to use Notifire. It integrate...
React Native 0.80 & ExecuTorch: A Powerful Leap into Offline AI for Mobile Apps
Jul 28, 2025
🚀 What’s New in React Native 0.80? The React Native 0.80 release marks a pivotal moment in mobile development. This update not only enhances...
Mastering Laravel Context: Advanced Logging and Contextual Job Tracing
Jul 20, 2025
Laravel Context is one of the most powerful new features in Laravel 12. It allows you to attach contextual data (like user ID, IP address, request pat...