Laravel 11 Changes You Need To Know
1. Slim Skeleton
When you install a new Laravel project, the folder structure will look like this:
app/
├── Http/
│ └── Controllers/
│ └── Controller.php
├── Models/
│ └── User.php
└── Providers/
└── AppServiceProvider.php
bootstrap/
├── app.php
└── providers.php
config
...
Removed folders: app/Console
, app/Exceptions
, app/Http/Middleware
.
Routes, Middlewares, and Exceptions are now registered in the bootstrap/app.php
file.
bootstrap/app.php:
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();
The files routes/channel.php
, routes/console.php
, and routes/api.php
have been removed.
2. Removed Some Config Files
Config files like broadcasting.php, cors.php, sanctum.php, hashing.php, view.php are removed but you can publish them using artisan command
php artisan config:publish
or:
php artisan config:publish --all
3. API and Broadcasting: Installed Optionally
Laravel 11 has no routes/api.php,routes/channel.php,
routes/console.php installed by default. We now have to install those using artisan conmmands like
php artisan install:api
php artisan install:broadcast
4. New Defaults: Pest and SQLite
Pest is now default testing framework in laravel and default database is now SQlite.
5. New “make:xxxxx” Commands
Some new artisan commands are added in laravel to create enums, interface and classes
php artisan make:enum
php artisan make:class
php artisan make:interface
6. Minimum PHP 8.2
Laravel 11 dropped support for PHP 8.1. Now, PHP 8.2 is the minimum requirement.
7. Casts Method
The casts will be provided with protected method casts()
instead of the protected property $casts
.
class User extends Authenticatable
{
// ...
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
}
// Now
protected function casts(): array
{
return [
'bookOptions' => [AsCollection::class, OptionCollection::class],
];
}
8. Limit Eager Load
Now laravel 11 will allow limiting eagerlyl loaded records. For Example
$users = User::with(['posts' => function ($query) {
$query->latest()->limit(10);
}])->get();
9. Health Check
Laravel 11 comes with a new registered Route /up
. The /up
endpoint returns the response time.
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
channels: __DIR__.'/../routes/channels.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
//
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
10. once() Method
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 Post extends Model
{
public function stats(): array
{
return once(function () {
// Expensive operations to generate post stats, multiple db queries, etc...
return $stats;
});
}
}
// Assuming you have two post instances: $postA and $postB
$postA->stats();
$postA->stats(); // cached result from previous line...
$postB->stats(); // Not using $userA cached results, because $postB !== $postA
$postB->stats(); // cached result from previous line...