The wait is over now!
Laravel 11 has been one of the most exciting versions for a long time. There are various major changes and new features announced with Laravel v11.
Let’s explore them…
As per the announcement by Taylor Otwell at Laracon EU, Laravel 11 introduces
So stay tuned, we’ll cover all of these here…
Along with Laravel 11, they had announced the release of Laravel Reverb.
Reverb is the newest addition to the Laravel ecosystem which is the first-party scalable WebSocket server. The purpose of its introduction was to provide robust real-time capabilities to the Laravel applications. We’ll cover this new addition later in this blog…
Laravel v11 brings a new layout to the Welcome page. Check out the below images.
Welcome page in both light and dark mode.
Laravel 11 has introduced a minimal application structure for new Laravel applications, without requiring any change to existing applications.
When you install a new Laravel project, the application’s folder structure will look like this…
app/
├── Http/
│ └── Controllers/
│ └── Controller.php
├── Models/
│ └── User.php
└── Providers/
└── AppServiceProvider.php
bootstrap/
├── app.php
└── providers.php
config
...
Here are the removed folders: app/Console
, app/Exceptions
, app/Http/Middleware
Routes, Middlewares, and Exceptions are now registered in the bootstrap/app.php
file.
This is how bootstrap/app.php
file looks:
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
//
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
In addition, the routes folder has been simplified; the api.php
and channels.php
route files are no longer present by default since many applications do not require these files.
At first, when Taylor Otwell introduced a minimal application structure, all config/
files were removed. However, he didn’t totally agree with this change and introduced a minimal config version.
If you look at the config/
folder of Laravel 10, you will find these files missing in Laravel 11:
config/broadcasting.php
config/cors.php
config/hashing.php
config/sanctum.php
config/view.php
But you can publish them manually, with this command:
php artisan config:publish
Or
php artisan config:publish --all
Also, some config values inside the existing files were removed in favour of automation inside the framework itself.
Now, let’s talk about Sanctum and Broadcasting configuration…
Laravel 11 has no routes/api.php
file, and Sanctum isn’t installed by default. You can install the API scaffolding with the php artisan install:api
artisan command.
When you run this artisan command, the routes/api.php
file will be created and registered in the bootstrap/app.php
, and Laravel Sanctum will be installed. You must only add the Laravel\Sanctum\HasApiTokens
trait to the User
Model.
Similarly to the API changes, broadcasting also became installable: to prepare the application, you can use the php artisan install:broadcast
artisan command.
With Laravel v11 you will get a few defaults set in your application.
In the pole created by Taylor on X.com, people voted for Pest to be the default testing framework. So they changed it to Pest in Laravel v11.
In Laravel 11, the default database was changed to SQLite. So the new Laravel applications will use SQLite for database storage, as well as the database driver for Laravel’s session, cache, and queue.
Laravel v11 comes with new make:
artisan commands. Now, you can create enums, interfaces, and classes.
php artisan make:enum
php artisan make:class
php artisan make:interface
New Laravel 11 applications include a health routing directive, which instructs Laravel to define a simple health-check endpoint that may be invoked by third-party application health monitoring services or orchestration systems like Kubernetes. By default, this route is served at '/up:'
Laravel 11 introduces a new Dumpable
trait. It is intended to replace the current dd()
and dump()
methods in most of the framework’s classes. Users and package authors can include this trait for easier code debugging.
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Dumpable;
class Address
{
use Conditionable, Dumpable;
// ...
}
$address = new Address;
$address->setThis()->setThat();
$address->setThis()->dd()->setThat();
Laravel 11 will allow limiting eagerly loaded records natively, without external packages.
class User extends Model
{
public function posts()
{
return $this->hasMany(Post::class);
}
}
class Post extends Model
{
// ...
}
$users = User::with(['posts' => function ($query) {
$query->latest()->limit(10);
}])->get();
Before Laravel 11, you were using a separate package Eloquent Eager Limit by Jonas Staudenmeir.
From Laravel 11, the casts will be provided in the protected method casts()
instead of the protected property $casts
.
class User extends Authenticatable
{
// ...
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
}
Also, now it is possible to provide an array.
protected function casts(): array
{
return [
'bookOptions' => [AsCollection::class, OptionCollection::class],
];
}
This is a non-breaking change. You only need to know that the casts()
method is prioritized over the $casts
property.
There’s a new memoization function called once()
. This function ensures that a callable is called only once, returning the same result on subsequent calls.
class User extends Model
{
public function stats(): array
{
return once(function () {
// Expensive operations to generate user stats, multiple db queries, etc...
return $stats;
});
}
}
$userA->stats();
$userA->stats();
$userB->stats();
$userB->stats();
Finally, Laravel 11 has dropped support for PHP 8.1. Now, PHP 8.2 is the minimum requirement.
Laravel Reverb brings blazing-fast and scalable real-time WebSocket communication directly to your Laravel application and provides seamless integration with Laravel’s existing suite of event broadcasting tools, such as Laravel Echo.
Futhermore, Reverb supports horizontal scaling via Redis’s publish / subscribe capabilities, allowing you to distribute your WebSocket traffic across multiple backend Reverb servers all supporting a single, high-demand application.
Here is an example of a stress test with ~30,000 clients maintaining open connections to Reverb, where each connection is subscribed to 10 different channels, and more than 6,000 messages are being exchanged per second:
Source: Laravel Blog
Additionally, you may monitor the performance of your Reverb servers in Laravel Pulse to get a better understanding of the number of connections and messages being handled.
We encourage you to check out the release notes and upgrade guide for more information on the new features and improvements in Laravel 11.
Subscribe to our newsletter and learn about the latest digital trends.