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
Laravel 12: What’s New From 12.0 to 12.19 – A Complete Guide
Jul 20, 2025
🔧 1. Laravel 12.0 – Starter Kits & Core Changes Version 12.0 introduced modern starter kits for React, Vue, Livewire, plus integratio...
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...
React Hooks Complete Guide
Jul 01, 2025
🎣 Complete React Hooks Guide with Practical Examples 🧠 useState What it does: Adds local state to a function component. Code Example: impo...
Laravel 12.19: Elegant Query Builders with PHP Attributes
Jul 07, 2025
Laravel 12.19: Elegant Query Builders with PHP Attributes In Laravel 12.19, you can now use the #[UseEloquentBuilder] PHP attribute to assign a cus...
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...
Using useCallback in React: Benefits, Limitations, Best Practices
Jul 31, 2025
The useCallback hook in React is used to return a memoized version of a callback function, so it's only recreated when its dependencies change. The...
