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
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....
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...
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...
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...
Laravel Queue & Job System: From Table Creation to Production Deployment
Jul 01, 2025
🚀 Laravel Queue & Job System We’re gonna walk you through Laravel queues from setup to deploying in production using Supervisor. Step 1...
