Laravel 11: Towards the Future of Web Development

Laravel 11: Towards the Future of Web Development

When will Laravel 11 be released

According to the Support Policy, Laravel 11 is expected to be released on February 6, 2024.

However, the release of Laravel 11 does not mean you have to immediately update all your projects.

Laravel only has Long-Term Support (LTS) versions in version 6, but each major version has two years of updates, giving you enough time to review the source code and upgrade.

Laravel 10 will continue to receive bug fixes until August 6, 2024, and security fixes until February 4, 2025.

Version PHP Release Date Bug Fixes Until Security Fixes Until
10 8.1 February 14, 2023 August 6, 2024 February 4, 2025
11 8.2 Q1 2024 August 5, 2025 February 3, 2026

How to install Laravel 11

Currently, Laravel 11 has not been released. Therefore, you must use the --dev flag on the official Laravel installation tool. This flag will pull the source code from the main branch of the laravel/laravel repository, always containing the latest code.

laravel new app --dev

Alternatively, if you prefer using Composer:

composer create-project --prefer-dist laravel/laravel app dev-master
Laravel 11
Laravel 11

 

What's new in Laravel 11?

Laravel 11 will no longer support PHP 8.1.

Upon the release of Laravel 11, PHP 8.2 will be established, and PHP 8.3 will also be stable. With support for the last two major versions of PHP, Laravel can move forward and abandon 8.1.

However, remember that your Laravel applications do not need to be updated immediately when the latest version is released.

This is especially important if you have projects with fees for customers or employees who depend on them to perform their work.

Such projects need to transition gradually but surely, with thorough testing. Don't rush.

Check the GitHub PR: Drop PHP 8.1 support

Laravel 11 introduces a simpler application template.

Laravel 11 comes with a simpler application template. The idea is to have less boilerplate code for you to handle. I completely agree with this concept. Here are the details of these changes:

  • In AuthServiceProvider, the $policies property has been removed because the framework will automatically detect them.
  • In EventServiceProvider, there's no need to have SendEmailVerificationNotification anymore, as EventServiceProvider fundamentally registers it. You'll also notice that automatic event discovery is now activated by default.
  • BroadcastServiceProvider is no longer necessary and has been removed. The framework will no longer automatically load the routes/channels.php file.
  • RedirectIfAuthenticated is now simpler due to having a basic version within the framework.
  • Middleware Authenticate no longer calls redirectTo() for JSON routes. This eliminates an unnecessary if-else check.
  • Various middleware such as EncryptCookies, PreventRequestsDuringMaintenance.php, TrimStrings, TrustHosts, TrustProxies, ValidateCsrfToken, and ValidateSignature have been removed from the template.
  • Custom Artisan commands will be loaded automatically. The console kernel no longer needs to call the load() method.
  • The routes/console.php file has been removed. Artisan commands based on Closure can be registered in the console kernel.
  • Some migrations have been consolidated into a single file or simply removed.
  • Traits AuthorizesRequests and ValidatesRequests have been removed from the basic controller.
  • The bootstrap/app.php file has been minimized to just three lines of code.
  • The exception handler has been removed.

Here's the PR for Laravel 10 (Simple Application Template), which was then moved to Laravel 11. You'll find more information about what has changed.

Check the PR on GitHub: Laravel 10 PR (Simple Application Template)

Laravel 11 introduces a new feature: the Dumpable trait (enabling the use of dump() and dd() on your objects).

This pull request introduces a new Dumpable trait in Laravel 11, aiming to replace the current dd and dump methods in most framework classes.

This trait allows Laravel users and package authors to easily include debug methods in their classes by using this trait.

Here's an example code snippet showing how to use it:

<?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();

Check this PR on GitHub: Add Dumpable trait

New feature: Improved Model::casts() method introduced in Laravel 11.

Typically, in Laravel, you declare attribute casting in an Eloquent model like this:

class User extends Model
{
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

With Laravel 11, you can define casting through the casts() method in your model, allowing you to use static methods from the casting class. Here's how to do it:

class User extends Model
{
    protected function casts(): array
    {
        return [
            'foo' => AsCollection::using(FooCollection::class),
        ];
    }
}

Moreover, you can now specify your 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],
        ];
    }
}

The casts() method takes precedence over the $casts property.

All these changes are not very disruptive, as they will not affect your current code if you upgrade to Laravel 11.

Check the PR on GitHub: Add Model::casts() method

In summary, the new features in Laravel 11 will provide developers with more power and flexibility when developing web applications. With various new features such as enhanced support for Telescope, more utility functions for jobs, improvements in Blade Drawings, and additional convenient features, Laravel is sure to continue its popularity.

Tags: