Laravel, one of the most popular PHP frameworks, continues to evolve with each release. The introduction of Laravel 11 brings significant improvements and changes that developers must understand to stay ahead of the curve. In this guide, we will delve into every major update and feature in Laravel 11, ensuring that your development practices are up-to-date and optimized for performance.
Introduction
Laravel 11, released in February, marks a new chapter for developers with a host of improvements and changes designed to streamline the development process. Whether you’re upgrading from a previous version or starting a new project, this guide will equip you with everything you need to know about Laravel 11.
PHP Version Requirements
Laravel 11 requires a minimum PHP version of 8.2. This ensures compatibility with the latest PHP features and performance enhancements. If you are currently using PHP 8.3 or higher, you are already prepared to run Laravel 11 without any issues.
Installation of Laravel 11
To install Laravel 11, you have two options depending on the release status:
Pre-release Installation:
laravel new laravel-11 --dev
or using Composer:
composer create-project --prefer-dist laravel/laravel laravel-11 dev-master
Post-release Installation:
composer create-project --prefer-dist laravel/laravel laravel-11
These commands will set up a Laravel 11 project, allowing you to start exploring its features immediately.
Redesigned Welcome Page
Laravel 11 introduces a redesigned “Welcome” page. While this change might seem minor, it reflects Laravel’s commitment to continually refining even the smallest details of the user experience. The new design is modern and better aligns with the overall Laravel aesthetic.
Kernel Modifications: HTTP and Console
Significant changes have been made to the Kernel in Laravel 11:
- HTTP Kernel: The
Kernel
class, traditionally used for registering middleware, has been removed. Middleware registration now occurs directly in the/bootstrap/app.php
file.
return Application::configure()
->withProviders()
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
)
->withMiddleware(function (Middleware $middleware) {
// Middleware registration
})
->withExceptions(function (Exceptions $exceptions) {
// Exception handling
})->create();
- Console Kernel: The
app/Console/Kernel.php
file has been removed, and routes that were traditionally handled by this file are now managed inroutes/console.php
.
Routing Changes
Laravel 11 simplifies routing by reducing the default routes to just console.php
and web.php
. If additional routes like channels.php
or api.php
are needed, they can be created manually or with Artisan commands:
php artisan install:api
php artisan install:broadcasting
After creating these routes, they can be enabled in bootstrap/app.php
:
return Application::configure()
->withProviders()
->withRouting(
web: __DIR__.'/../routes/web.php',
api: __DIR__.'/../routes/api.php',
commands: __DIR__.'/../routes/console.php',
channels: __DIR__.'/../routes/channels.php',
)
->withMiddleware(...)
->withExceptions(...)->create();
Enhanced Model Casting
Laravel 11 introduces a new way to handle model casting, providing more flexibility and control:
Old Way:
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
New Way:
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
This change allows for more complex casting scenarios, including the use of custom classes or functions.
The Introduction of Dumpable Trait
Laravel 11 introduces the Dumpable
trait, simplifying the debugging process. This trait can be added to classes, providing access to the dd
and dump
methods directly within the class context:
class Carbon extends BaseCarbon
{
use Conditionable, Dumpable;
}
This trait can be applied to any class, including models, enhancing the flexibility of debugging and data inspection.
Controller Simplifications
The Controller.php
file in Laravel 11 has been simplified. It no longer extends BaseController.php
, resulting in a cleaner and more straightforward controller structure:
abstract class Controller
{
//
}
This change reflects Laravel’s ongoing effort to reduce boilerplate and unnecessary complexity in its codebase.
Minimalist App Structure
Laravel 11 continues the trend towards minimalism by reducing the number of default files and folders in the app
directory. The default structure now includes only three files and four folders:
app
├── Http
│ └── Controllers
│ ├── Controller.php
├── Models
│ └── User.php
└── Providers
└── AppServiceProvider.php
This streamlined structure simplifies project organization and reduces clutter, making it easier to navigate and manage your codebase.
Config Directory Overhaul
In previous versions of Laravel, the config
directory contained numerous configuration files, many of which were rarely modified. Laravel 11 reduces the default config
files to those most commonly adjusted:
config
├── app.php
├── auth.php
├── cache.php
├── database.php
├── filesystems.php
├── logging.php
├── mail.php
├── queue.php
├── services.php
└── session.php
If additional configuration files are needed, they can be published using the Artisan config:publish
command.
Slim Migration Setup
New Laravel 11 projects will include a slimmed-down migration setup with only three default migration files:
database/migrations
├── 0001_01_01_000000_create_users_table.php
├── 0001_01_01_000001_create_cache_table.php
└── 0001_01_01_000002_create_jobs_table.php
This change reduces the initial clutter in the migrations
directory and focuses on the most essential database structures.
Integration with External Packages
Laravel 11 comes with built-in integration for packages like the one developed by Jonas Staudenmeir, enhancing the management of query scopes in eager loading:
User::query()->select('id', 'name')->with([
'statuses' => fn($query) => $query->take(5)
])->get();
This integration makes it easier to write efficient and concise query logic directly within your models.
New Artisan Make Commands
Laravel 11 introduces several new Artisan commands to streamline development:
make:enum
: Generates a new enum class:
php artisan make:enum Enums/ArticleStatus -s
make:trait
: Creates a new trait:
php artisan make:trait Traits/Sluggable
These commands simplify the creation of commonly used structures, reducing the time spent on boilerplate code.
Upgrading from Laravel 10 to 11
Upgrading from Laravel 10 to 11 is straightforward. Simply update the Laravel version in your composer.json
file and ensure that all third-party packages are compatible with the new version. The directory structure changes will not require any code modifications, making the upgrade process smooth and hassle-free.
Conclusion
Laravel 11 is a powerful update that continues the framework’s legacy of innovation and developer-friendliness. With its streamlined structure, enhanced features, and modern approach, Laravel 11 is set to be the preferred choice for developers aiming to build robust and scalable web applications. We encourage you to explore these new features and integrate them into your projects to take full advantage of what Laravel 11 has to offer.