Laravel continues to evolve, bringing speed, elegance, and developer joy with every release. With Laravel 12, the framework introduces a range of new features and enhancements that aim to streamline development, improve performance, and modernize your tech stack. If you’re currently using Laravel 11, this post will guide you through the most significant changes in Laravel 12 with clear examples and insights.
In this blog post, we’ll explore 12 powerful new features introduced in Laravel 12. Whether you’re a seasoned developer or someone looking to upgrade from Laravel 11, this guide will help you understand the changes and how to leverage them in your next project.
1. New Starter Kits with Modern Frontend Stack
Laravel 12 introduces revamped starter kits for React, Vue, and Livewire. These kits now come pre-integrated with TypeScript, Tailwind CSS, shadcn/ui, and authentication systems.
Highlights:
- Auth out-of-the-box (traditional login, social, SSO, passkeys)
- TypeScript setup
- Cleaner frontend integration using Inertia.js
Example:
php artisan breeze:install react
npm install && npm run dev
php artisan migrate
This command scaffolds a fully functional React + Inertia app with Laravel backend ready for authentication.
2. Performance Enhancements
Laravel 12 focuses on reducing boot time and memory usage. Improvements in route resolution, service container bindings, and bootstrapping translate into faster applications.
Key Improvements:
- Faster route caching
- Optimized service container resolution
- Reduced memory usage in large applications
Tip:
Run the following to cache your configuration and routes for production:
php artisan config:cache
php artisan route:cache
3. Improved Testing Tools
Testing in Laravel 12 is more robust than ever with new assertions and enhanced parallel testing.
New Assertion:
public function testDatabaseHasExactlyExample()
{
$this->assertDatabaseHasExactly('users', [
['email' => 'admin@example.com'],
['email' => 'user@example.com']
]);
}
Run Tests in Parallel:
php artisan test --parallel
This drastically reduces test suite execution time, especially in CI/CD environments.
4. New Artisan Commands
Laravel 12 brings two helpful artisan commands:
1. Make Enum:
php artisan make:enum UserRole
This creates an enum in app/Enums/UserRole.php
enum UserRole: string {
case Admin = 'admin';
case User = 'user';
}
2. Model Pruning:
Automatically delete old or soft-deleted records.
class Session extends Model
{
use Prunable;
public function prunable()
{
return static::where('created_at', '<=', now()->subMonth());
}
}
php artisan model:prune
5. Stronger Type Safety with Strict Mode
Laravel 12 introduces a strict mode that enforces type checking for better consistency.
Example:
In your config:
// config/app.php
'strict_types' => true,
Framework methods now return typed responses, reducing runtime bugs:
public function getUserId(): int
{
return $this->user->id;
}
6. Revamped Queue System
Laravel’s new queue worker supports auto-scaling and job prioritization with improved Horizon integration.
Auto-scaling Example with Horizon:
'environments' => [
'production' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['emails', 'default'],
'balance' => 'auto',
'processes' => 5,
'tries' => 3,
],
],
],
Laravel Horizon automatically scales workers based on queue load.
7. Full Support for PHP 8.2 & 8.3
Laravel 12 is fully compatible with PHP 8.2 and 8.3, supporting features like readonly classes, native enums, and better type safety.
PHP 8.2 Example:
readonly class CartItem
{
public function __construct(
public string $name,
public float $price
) {}
}
Native Enums Example:
enum OrderStatus: string {
case Pending = 'pending';
case Shipped = 'shipped';
}
8. Streamlined Dependency Injection
With property promotion and constructor simplification, Laravel 12 makes dependency injection cleaner and more intuitive.
Before:
class ProductController extends Controller
{
protected $service;
public function __construct(ProductService $service)
{
$this->service = $service;
}
Now:
class ProductController extends Controller
{
public function __construct(protected ProductService $service) {}
}
9. Advanced Query Builder Enhancements
The Query Builder now includes methods like nestedWhere()
for more elegant query chaining.
Example:
User::query()
->where('active', true)
->nestedWhere(function ($query) {
$query->where('role', 'admin')
->orWhere('email_verified_at', '!=', null);
})
->get();
10. Refined Application Structure
Laravel 12 introduces a more refined file and directory structure:
bootstrap/app.php
is simplifiedroutes/console.php
androutes/channels.php
are optional- Modular organization is encouraged using
Modules
orDomains
This helps in maintaining and scaling large applications with more clarity.
11. Enhanced Security Features
Security is a top priority in Laravel 12, with updates to:
- CSRF Protection: Token regeneration is more robust
- Rate Limiting: More flexible with custom responses
- Encryption: Modern ciphers and fallback strategies
Example:
Rate limit with custom response:
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->response(function () {
return response('Slow down!', 429);
});
});
12. Minimal Breaking Changes
Laravel 12 ensures high backward compatibility. Most Laravel 11 applications can be upgraded with minimal changes.
Upgrade Tip:
Use Laravel Shift or follow the upgrade guide to ensure smooth transition.
Conclusion
Laravel 12 is a thoughtful and powerful update that enhances both the developer and user experience. With features like stricter typing, modern frontend starter kits, performance boosts, and improved testing, it sets a strong foundation for building modern web applications.
Whether you’re upgrading or starting fresh, Laravel 12 will help you build better, faster, and more secure applications.
What’s your favorite Laravel 12 feature? Let me know in the comments below!
Need help upgrading your Laravel 11 app or planning a new Laravel 12 SaaS idea? Feel free to reach out!
Leave a Reply