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
Bypassing $fillable Safely with forceFill() in Laravel
Jul 02, 2025
Bypassing $fillable Safely with forceFill() in Laravel Ever used create() in Laravel and noticed some fields like role or status didn’t save? T...
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...
Mastering Async Iteration in JavaScript with Array.fromAsync()
Jul 27, 2025
🔍 What Exactly is Array.fromAsync()? Array.fromAsync() is a static method introduced in ES2024 as part of JavaScript's growing support for asynchr...
Mastering Modern CSS: The Power of if(), Popover Hints, and Smart Styling
Jul 16, 2025
🌐 Mastering Modern CSS: The Power of if(), Popover Hints, and Smart Styling CSS is getting smarter. In this guide, we’ll explore how the new...
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...
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....
