Laravel 12.19: Elegant Query Builders with PHP Attributes
July 7, 2025Laravel 12.19: Elegant Query Builders with PHP Attributes
In Laravel 12.19, you can now use the #[UseEloquentBuilder]
PHP attribute to assign a custom query builder to a model—no need to override newEloquentBuilder()
anymore. It's cleaner and more maintainable.
Why Use a Custom Query Builder?
- Keep models lean—models focus on relationships, not query logic.
- Centralize reusable logic like
wherePublished()
in one place. - Organize code better—all custom queries live together.
The Old Way
// app/Models/Article.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Builders\ArticleBuilder;
class Article extends Model {
public function newEloquentBuilder($query) {
return new ArticleBuilder($query);
}
}
The New Way
// app/Models/Article.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Builders\ArticleBuilder;
use Illuminate\Database\Eloquent\Attributes\UseEloquentBuilder;
#[UseEloquentBuilder(ArticleBuilder::class)]
class Article extends Model {
// no more override!
}
Builder Example
// app/Builders/ArticleBuilder.php
namespace App\Builders;
use Illuminate\Database\Eloquent\Builder;
class ArticleBuilder extends Builder {
public function whereActive(): static {
return $this->where('is_active', true);
}
public function recent(int $days = 7): static {
return $this->whereDate('created_at', '>=', now()->subDays($days));
}
public function byCategory(string $slug): static {
return $this->whereHas('category', fn($q) => $q->where('slug', $slug));
}
}
Usage
$articles = Article::query()
->whereActive()
->recent(14)
->byCategory('tech')
->orderByDesc('published_at')
->paginate(10);
Pro Tips
- ✅ Only use when query logic gets reused.
- ⚙️ Keep method names flexible, e.g.
recent(int $days)
. - 🧪 Add unit tests for each builder method.
- 📘 Consider combining with global scopes.
This feature was introduced in Laravel 12.19 along with other improvements like AsFluent
, FailOnException
job middleware, and new response assertion methods.
Blog
React History – A Professional Developer’s Perspective
Jul 26, 2025
1. Origins: Born Inside Facebook In 2011, Facebook engineers faced the increasing complexity of building interactive UIs at scale. They developed...
Color Everything in CSS – Simple Guide
Jun 26, 2025
Color Everything in CSS – Simple Guide Today we’re diving into CSS colors: how to define them, especially with modern methods like lab(...
How OAuth Works
Jun 29, 2025
How OAuth Works OAuth is a protocol that allows third-party applications to access user data without sharing passwords. It's the backbone of secure a...
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...
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...
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...
