Ever feel like Laravel is that sneaky friend who knows all the shortcuts but only tells you some of them? 🤔
You’re busy writing controllers, debugging queries, and maybe sipping your third cup of coffee, and Laravel is quietly offering little tricks that could save you hours of work. Even seasoned developers sometimes miss these gems.
Lucky for you, we’re going on a mini treasure hunt. 🕵️♂️ But instead of gold, you’ll discover 10 hidden Laravel 12 features that make coding faster, cleaner, and, let’s be honest, a little more fun.
By the end of this post, you’ll be writing shorter, smarter, and more elegant Laravel code—and maybe impress your team while doing it.
1️⃣ How to Use Route Model Binding with Custom Keys
By default, Laravel resolves models using id. But you can bind models using other columns, like username or slug.
Route::get('users/{user:username}', [UserController::class, 'show']);
public function show(User $user) {
return $user;
}
✅ Pro Tip: This keeps your controllers clean and readable.
Learn more: Laravel Route Model Binding Docs
2️⃣ Why Scoped Nested Route Bindings Matter
Working with nested resources? Route::scopeBindings() ensures child models are only looked up within their parent.
Route::scopeBindings()->group(function () {
Route::get('teams/{team}/members/{user}', [TeamMemberController::class, 'show']);
});
No extra queries. No messy checks. Just clean, scoped routes.
Reference: Laravel Nested Resource Routing
3️⃣ How Eloquent Castable Objects Simplify Your Code
Want database values to be smart objects automatically? Use custom cast classes.
php artisan make:cast Money
protected $casts = ['price' => Money::class];
Now price is a Money object, not just a number.
More info: Laravel Eloquent Attribute Casting
4️⃣ How the Query Builder when Helper Keeps Queries Clean
Instead of wrapping queries in if statements, use the when helper:
$users = User::query()
->when($role, fn($q) => $q->where('role', $role))
->get();
✅ Tip: Keeps your queries short, readable, and maintainable.
Read more: Laravel Query Builder
5️⃣ When to Use Model::withoutEvents
Sometimes you don’t want model events firing—like during bulk imports or testing.
User::withoutEvents(function () {
User::create([...]);
});
Prevents unwanted notifications, observers, or side effects.
Reference: Laravel Model Events
6️⃣ How to Dispatch Jobs Synchronously
Need a job to run immediately instead of queueing? Use dispatchSync or dispatchNow.
use Illuminate\Support\Facades\Bus;
Bus::dispatchSync(new ProcessOrder($order));
Perfect for tests or quick workflows.
Learn more: Laravel Queues
7️⃣ How Blade @once and @pushOnce Prevent Duplicates
Loops and nested components can repeat scripts or HTML. Use Blade directives:
- @once – run code only once:
@once
<script src="/js/app.js"></script>
@endonce
- @pushOnce – prevent duplicate pushes:
@pushOnce('scripts')
<script src="/js/app.js"></script>
@endPushOnce
✅ Saves headaches in bigger projects.
Reference: Laravel Blade Directives
8️⃣ How to Bypass Maintenance Mode with a Secret
Laravel lets certain users access your app during maintenance using a secret:
php artisan down --secret="letmein123"
Then visit:
/?secret=letmein123
Great for testing or quick demos.
More info: Laravel Maintenance Mode
9️⃣ How Invokable Controllers Simplify Single-Action Routes
Controllers with only one action can be made invokable:
php artisan make:controller ShowProfileController --invokable
Route::get('profile', ShowProfileController::class);
No method names required. Laravel automatically calls __invoke().
Reference: Laravel Controllers
🔟 How Lazy Collections Handle Huge Datasets
Processing large datasets can crash memory. Use Lazy Collections instead:
use Illuminate\Support\LazyCollection;
LazyCollection::make(function () {
foreach (DB::table('big_table')->cursor() as $row) {
yield $row;
}
})
->filter(...)
->each(...);
Perfect for handling millions of rows efficiently.
Learn more: Laravel Lazy Collections
Key Points
- Custom keys in route model binding
- Scoped nested routes for safe lookups
- Cast classes for richer DB objects
- Query Builder
whenhelper for clean code withoutEventsto skip model events- Dispatch jobs synchronously
- Blade @once/@pushOnce to prevent duplicates
- Maintenance mode secrets
- Invokable controllers for single-action routes
- Lazy collections for memory-efficient processing