Laravel 11 - Big Changes And New Features
When was Laravel 11 released?
Laravel 11 was released on March 12, 2024.
Adopting a new major version should never be rushed.
The previous Laravel LTS (Long-Term Support) version was 6, but each major version receives two years of updates, giving you ample time to test and upgrade your codebase.
According to the Support Policy, Laravel 10 will receive bug fixes until August 6, 2024, and security fixes until February 4, 2025.
Version PHP | PHP | Release Date | Bug Fixes | Until Security Until |
---|---|---|---|---|
10 | 8.1 | February 14, 2023 | August 6, 2024 | February 4, 2025 |
11 | 8.2 | 8.2 March 12, 2024 | September 3, 2025 | March 12, 2026 |
Upgrading a project to Laravel 11 or creating a new one
To upgrade a project to Laravel 11, follow its simple upgrade guide full of helpful tips.
And to create a new Laravel 11 project, grab the official installer and run:
laravel new laravel11
Or, if you prefer using Composer:
composer create-project laravel/laravel laravel11
What's new in Laravel 11?
A leaner application structure
With the introduction of Laravel 11, it was time to clean up and redefine how a Laravel application is structured. The goal? To provide a smaller and more efficient starting point for your projects. And the Laravel team really delivered on that.
Maybe you are interested: Laravel 11: Towards the Future of Web Development
An all-new bootstrap/app.php file
At the heart of this revamp is the reborn bootstrap/app.php
file, designed to act as your central command station. Here, you can tweak your application's routing, middleware, service providers, exception handling, and more—all from one location. Think of it as the starship's captain's chair.
Streamlined service providers
Accustomed to dealing with multiple service providers? Laravel 11 says, "No more!" Now, there's a single AppServiceProvider remaining. This change consolidates the functionalities of the old service providers into bootstrap/app.php or AppServiceProvider, making your codebase tidier.
Optional API and broadcast route files
Not every application needs API and broadcast features right from the start. Laravel 11 acknowledges this by not including the api.php and channels.php route files by default. Need them? A simple Artisan command away. Laravel's flexibility shines through, allowing you to add features only when required.
php artisan install:api
php artisan install:broadcasting
Default middleware classes are gone
The days of a cluttered middleware directory are over. Laravel 11 has moved these middleware into the framework itself, allowing you to enjoy a clean application structure while still customizing middleware behavior from bootstrap/app.php.
->withMiddleware(function (Middleware $middleware) {
$middleware->redirectGuestsTo('/admin/login');
})
Exception handling has also moved to bootstrap/app.php
In the spirit of consolidation, exception handling has also been moved to bootstrap/app.php. This keeps your application structure lean and means you don't have to hunt through multiple files to manage exceptions.
Here's a code snippet from the Laravel 11 release notes (bootstrap/app.php):
->withExceptions(function (Exceptions $exceptions) {
$exceptions->dontReport(MissedFlightException::class);
$exceptions->reportable(function (InvalidOrderException $e) {
// ...
});
})
Schedule jobs directly in routes/console.php
Scheduling jobs is now as simple as adding a few lines to your routes/console.php file, thanks to the new Schedule facade. No more console kernel needed.
In routes/console.php:
use Illuminate\Support\Facades\Schedule;
Schedule::command('service:sync')->daily();
A streamlined base controller class
The base controller class in Laravel 11 has been significantly trimmed down. The AuthorizesRequests and ValidatesRequests traits are still there, but you'll have to take action to use them.
For instance, if you're using policies and want to check authorization, you'll need to include the AuthorizesRequests trait in your base controller class (app/Http/Controllers/Controller.php):
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
abstract class Controller
{
use AuthorizesRequests;
}
New health-check route
Laravel 11 introduces a new health-check route that allows you to perform various checks on different parts of your application and ensure everything is running smoothly.
This route can be defined in bootstrap/app.php like so:
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
When the route is accessed, an Illuminate\Foundation\Events\DiagnosingHealth event is fired.
Rate limiting per second
Rate limiting in Laravel is easy to set up. With Laravel 11, you can rate limit per second.
But why is this useful? Let's look at an example:
RateLimiter::for('invoices', function (Request $request) {
-return Limit::perMinute(120);
+return Limit::perSecond(2);
});
- If we limit the number of requests per minute, that means your users could also send 120 requests in a single second.
- But if we limit the number of requests per second, your users won't be able to send 120 requests in a single second, but they'll still be limited to 120 requests per minute.
A great change, right?
Exploring the new Model::casts() method
Traditionally in Laravel, you'd declare attribute casting in an Eloquent model like so:
class User extends Model
{
protected $casts = [
'email_verified_at' => 'datetime',
];
}
With Laravel 11, you can define casting through a casts() method on your model, allowing you to use static methods with arguments. Here's how it works:
class User extends Model
{
protected function casts() : array
{
return [
'foo' => AsCollection::using(FooCollection::class),
];
}
}
Moreover, you can also define the casting as an array:
class User extends Model
{
// Even in the old $casts property!
protected $casts = [
'foo' => [AsCollection::class, FooCollection::class],
];
protected function casts() : array
{
return [
'foo' => [AsCollection::class, FooCollection::class],
];
}
}
Note that the casts() method takes precedence over the $casts property.
All these changes won't impact your current code if you're upgrading to Laravel 11.
View the pull request on GitHub: PR
A handy new "Dumpable" trait
This pull request introduces a new Dumpable trait in Laravel 11, meant to replace the current dd and dump methods across most classes in the framework.
This trait allows Laravel users and package authors to easily include debugging methods in their classes by using this trait.
Here's some sample code to illustrate its usage:
<?php
namespace App\ValueObjects;
use Illuminate\Support\Traits\Dumpable;
use Illuminate\Support\Traits\Conditionable;
class Address
{
use Conditionable, Dumpable;
// ...
}
$address = new Address;
// Before:
$address->foo()->bar();
// After:
$address->foo()->dd()->bar();
View the pull request on GitHub: PR
Dropping support for PHP 8.1
PHP 8.2 has been established, and PHP 8.3 is now the latest PHP version. Laravel can move forward and drop support for 8.1.
But remember: Your Laravel application doesn't need to be upgraded to the latest version as soon as it's released. Especially if you have client-paid or employee-reliant projects depending on them to function.
Those projects need to move slowly but surely by performing thorough testing. Don't rush.
View the pull request on GitHub: PR
Thank you for taking the time to read!