From Laravel 10 to 11: What You Need to Know

Arlind Musliu Portrait
Arlind Musliu

May 6, 2024 · 4 min read · 286 views

Laravel Blogpost Image

Laravel, the most popular PHP framework, has recently released version 11 with a host of new features and improvements. If you're familiar with Laravel 10 and are looking to transition to the latest version, here's a breakdown of the key differences between Laravel 10 and Laravel 11. We've also written an easy-to-follow guide that explains the key features of Laravel 11 for beginners.

Differences between Laravel 10 and Laravel 11

Minimal Application Structure

In Laravel 11, a minimal application structure has been introduced for new applications. The app folder has been simplified, with the HTTP and Console kernels taken out.

APP folder comparison Laravel 10 11

Customizing middleware has been made simpler, and all providers are now in one directory. You can still customize things through the bootstrap/app.php file, like changing routing, middleware, service providers, and how exceptions are handled.

Laravel 11 boostrap app file

However, you can still bring those back with these simple Laravel commands:

php artisan install:api
 
php artisan install:broadcasting

SQLite By Default

New Laravel applications now default to using SQLite for database storage, as well as the database driver for session, cache, and queue. This simplifies the setup process by automatically creating the SQLite file and running initial database migrations. The use of SQLite for both local and production applications is supported due to the robustness of the database drivers.

Database comparison Laravel 10 11

Health Routing

Laravel 11 includes a health routing directive that defines a simple health-check endpoint at /up. This endpoint can be used by third-party health monitoring services or orchestration systems like Kubernetes. Additional health checks specific to your application can be performed through the DiagnosingHealth event triggered by requests to the health route.

Queue Interaction Testing

Testing queue interactions in Laravel 11 has been enhanced with the introduction of the withFakeQueueInteractions method. This method allows for testing the release, failure, or deletion of queued jobs easily. Assertions such as assertReleased, assertFailed, and assertDeleted can be used to verify job interactions.

Graceful Encryption Key Rotation

A new feature in Laravel 11 is the ability to define previous encryption keys via the APP_PREVIOUS_KEYS environment variable. This allows for graceful encryption key rotation without logging users out of the application. Laravel will attempt decryption using the current key first and then try previous keys if needed, ensuring uninterrupted application usage during key rotation. However, all new data will be encrypted with the new encryption key.

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:/w5RIS0/EY4aCBqYJgwR1eWfnn0Z5phOHEU1HuHPjlQ=
APP_PREVIOUS_KEYS="base64:UWL/rqvYtrdVugZN/nRDK8S/AHKDeN3g9Pc63j8oipM=,base64:hAyZ5U4bdBfrIwWySg7ptnLE2Q4IfjX9BtmamXxZsBM="
APP_DEBUG=true
APP_TIMEZONE=UTC
APP_URL=http://localhost

You can retain multiple previous encryption keys by separating them with commas in a list.

Other Improvements of Laravel 11

Laravel 11 introduces per-second rate limiting, Resend mail transport, Prompt validator integration, new Artisan commands for class creation, model casts improvements, the once function for caching callback results, improved performance when testing with in-memory databases, enhanced support for MariaDB, and database schema inspection methods. Here are some of them explained in more detail:

Per-Second Rate Limiting

In Laravel 11, developers can now set rate limits on a per-second basis, allowing for more precise control over how many requests can be made in a single second. This improvement helps applications manage traffic better and avoid situations where there is too much abuse or overload.

<?php

RateLimiter::for('orders', function (Request $request) {
    return Limit::perSecond(1);
});

Prompt Validator Integration

In Laravel 11, the Prompt validator is now included, allowing developers to improve command-line applications by adding user-friendly forms and validation features. This integration makes it easier to create interactive command-line interfaces that can validate user input, making the experience better for users.

<?php

$email = text('Enter your email:', validate: [
    'email' => 'email:rfc,dns'
]);

Laravel 11 New Artisan Commands

Laravel 11 introduces new Artisan commands for class creation, offering a streamlined approach to generating classes, enums, interfaces, and traits within Laravel applications. These commands simplify the process of creating new classes and components, enhancing developer productivity and code organization.

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

The Once Function for Caching Callback Results

The introduction of the once function in Laravel 11 enables developers to cache callback results for the duration of a request. This function optimizes performance by caching and reusing callback results, reducing redundant computations and improving application responsiveness.

<?php

function getRandomPosts(): array
{
    return once(function () {
        return Post::inRandomOrder()>take(5)>get();
    });
}
 
getRandomPosts(); // Post ids: 155,642,544,112,737
getRandomPosts(); // Post ids: 155,642,544,112,737 (cached result)
getRandomPosts(); // Post ids: 155,642,544,112,737 (cached result)

Database Schema Inspection Methods

Laravel 11 introduces database schema inspection methods that allow developers to retrieve information about tables, views, columns, indexes, and foreign keys within the database. These methods provide valuable insights into the database schema structure, facilitating database management and schema manipulation tasks.

<?php

use Illuminate\Support\Facades\Schema;
 
$tables = Schema::getTables();
$views = Schema::getViews();
$columns = Schema::getColumns('posts');
$indexes = Schema::getIndexes('posts');
$foreignKeys = Schema::getForeignKeys('posts');

Conclusion

Overall, Laravel 11 builds upon the foundation of Laravel 10 with a streamlined application structure, new features, and improvements aimed at enhancing developer experience and application performance. For more detailed information, refer to the Laravel 11 release notes and upgrade guide. Stay updated with the latest Laravel developments to make the most of this powerful framework for your projects. Happy coding! 🚀

Arlind Musliu Portrait
Arlind Musliu

Cofounder and CFO of Lucky Media

Technologies:

Laravel
Heading Pattern

Related Posts

Stay up to date

Be updated with all news, products and tips we share!