blog bg

March 17, 2024

Level Up with Laravel 11: Unveiling the Latest Features and Enhancements

test-img

@mert

Share what you learn in this blog to prepare for your interview, create your forever-free profile now, and explore how to monetize your valuable knowledge.

The Latest in Laravel Development:

 

Laravel 11 has officially launched, marking a significant milestone in web development. This groundbreaking release mandates a minimum PHP version of 8.2, introducing developers to a realm of enhanced capabilities and optimizations. Among the updates, the introduction of the innovative Laravel Reverb package stands out, promising to revolutionize the development experience. Additionally, a streamlined directory structure ensures efficiency and organization, reaffirming Laravel's commitment to delivering developer satisfaction and project success.

 

Laravel Reverb

 

Laravel Reverb is a new first-party WebSocket server for Laravel applications, bringing real-time communication between client and server. 

 

For additional details, don't forget to check out the video below!

 

Streamlined Directory Structure


After a fresh install, there's been a reduction of approximately 69 files. That's quite an improvement!

Some of the changes that can be easily spotted :

1) Controllers no longer extend anything by default.

  • 2) No more middleware directory.
  • 3) No Console Kernel.

 

Dive into the video below for a comprehensive exploration of the recent changes, gaining a better understanding of what has evolved:

 

Take a closer look at this link for additional insights:

 

Link:  https://laravel-news.com/laravel-11-directory-structure

 

Model casts changes

 

Model casts are now defined as a method instead of a property. When defined as a method we can do other things, like call other methods directly from the casts. Here is an example using a new Laravel 11 AsEnumCollection:

protected function casts(): array
{
    return [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
        'options'= AsEnumCollection::of(UserOption::class),
    ];
}

 

Discover the changes by watching the video below:

 

 

New Dumpable Trait

 

class Stringable implements JsonSerializable, ArrayAccess
{
    use Conditionable, Dumpable, Macroable, Tappable;
 
    str('foo')->dd();
    str('foo')->dump();

 

Read more about this: https://laravel-news.com/laravel-11-dumpable-trait 

 

Config Changes

 

Laravel traditionally includes numerous configuration files, but with Laravel 11, these have been consolidated and streamlined. Now, all configuration options cascade down, and the .env file has been expanded to incorporate all necessary settings.

 

Read more about this : https://laravel-news.com/laravel11-streamlined-configs 

 

New Once method

 

In Laravel 11, 'once' helper method has been introduced, guaranteeing consistent returns regardless of how many times an object method is called. This function is helpful when you have some code that you want to ensure only ever runs one time.

 

Curious about its practical application? Watch the video below to see how it can be utilized:

 

 

Slimmed default Migrations

 

When you start a new Laravel app, it comes with some default migrations from 2014 and 2019. These now will come with the dates removed and moved into just two files.

 

To see the changes, check out the reel: https://www.instagram.com/reel/C2w58cPA2eI/

 

Routes changes

 

By default, there will be only two route files, console.php and web.php. API routes will now become opt-in via php artisan install:api, giving you the API routes file and Laravel Sanctum.

The same with websocket broadcasting, php artisan install:broadcasting.

 

Health Route

 

Laravel 11 will include a new /up that fires a DiagnosingHealthEvent so you can better integrate with up time monitoring.

Link: https://laravel-news.com/laravel-11-health-endpoint 

 

Want to see how it can be used? Watch the video below:

 

 

APP_KEY Rotation

 

In older versions of Laravel, if you changed your APP_KEY it could lead to broken data in the database. Laravel 11 has a new graceful rotation which will NOT break old encrypted data, using an APP_PREVIOUS_KEYS comma-delimited list .env variable. It will auto re-encrypt the data using new key.

 

Check below video to see how this is done:

 

Named Arguments

 

Named Arguments are not covered by Laravel's backwards compatibility guidelines. They may choose to rename function arguments when necessary in order to improve the Laravel codebase. When calling Laravel methods using named arguments should be done cautiously and with the understanding that the parameter names may change in the future.

 

Are you not familiar with Named Arguments? then don't be shy check this below:

 

Link: https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments  

 

 

Eager Load Limit

 

Laravel 11 integrates the code behind the "eager load limit" package:

 

User::select('id', 'name')->with([
    'articles' => fn($query) => $query->limit(5)
])->get();

 

Read more about eager load limit: https://laravel-news.com/eager-load-limit 

 

New Artisan Commands

 

php artisan make:class
php artisan make:enum
php artisan make:interface
php artisan make:trait

 

 

Dont forget to check the welcome page : https://laravel-news.com/laravel-welcome-page 

 

Upgrade to Laravel 11

 

Link:  https://laravel.com/docs/master/upgrade#upgrade-11.0 

 

Laravel Support Policy

 

VersionPHP (*)ReleaseBug Fixes UntilSecurity Fixes Until
Laravel 98.0 - 8.2February 8th, 2022August 8th, 2023February 6th, 2024
Laravel 108.1 - 8.3February 14th, 2023August 6th, 2024February 4th, 2025
Laravel 118.2 - 8.3March 12th, 2024September 3rd, 2025March 12th, 2026
128.2 - 8.3Q1 2025Q3, 2026Q1, 2027

 

Source of this blog: https://laravel-news.com/laravel-11 

 

Laravel 11 is here with exciting updates! From a streamlined directory structure to the innovative 'once' helper method, model cast changes, dumpable trait and lots more, this release offers improved efficiency and convenience. Get ready to explore the latest features and enhancements. Happy coding!

260 views

Please Login to create a Question