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
ECMAScript 2025 Detailed Update Guide for Frontend Developers
Jul 06, 2025
🔍 ECMAScript 2025 – Detailed Feature Guide All new ECMAScript 2025 features with code examples and explanation of their importance for front...
Essential React Native UI & Interaction Components
Jul 01, 2025
Essential React Native UI & Interaction Components React Native provides a powerful set of built-in components for creating native mobile apps....
How to Make Your Website Blazing Fast – Step by Step
Jul 30, 2025
Why Performance Is Non-Negotiable In today’s fast-paced world, no one has time to wait for a slow-loading website. On mobile, users abandon...
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...
CSS Specificity: Layers vs BEM vs Utility Classes
Jun 26, 2025
CSS Specificity: Cascade Layers vs BEM vs Utility Classes This article compares three approaches to managing CSS specificity — BEM, utility‑f...
React Labs: View Transitions & Activity
Jun 17, 2025
React Labs: View Transitions & Activity Published April 23, 2025 by Ricky Hanlon. React Labs is sharing two new experimental featu...