DEV Community

Cover image for What's New in Laravel 9 - Features & Updates
sumyya khan
sumyya khan

Posted on • Edited on

What's New in Laravel 9 - Features & Updates

Laravel v9 is scheduled to be released around September of 2021, but the Laravel Crew decided to push this release back to January of 2022. Now Laravel has been released on 8 February 2022. The new version of the Laravel framework has been shipped with a variety of robust features and updates. Laravel 9 continues the improvements made in Laravel 8 by introducing support for Symfony 6.0 components, Symfony Mailer, Flysystem 3.0, improved route:list output, a Laravel Scout database driver, new Eloquent accessor/mutator syntax, implicit route bindings via Enums, and a variety of other bug fixes and usability improvements. Laravel 9.x requires a minimum PHP version of 8.0 and Flysystem 3.x.

Laravel 9 is the subsequent long-term support version (LTS) and will accept bug fixes until February 2024 and security fixes until February 2025. Here are some of the significant features that I want to share with you.

Flysystem 3.x

Laravel 9.x upgrades our upstream Flysystem dependency to Flysystem 3.x. Flysystem powers all of the filesystem interactions offered by the Storage facade.

Improved Eloquent Accessors/Mutators

Improved Eloquent accessors/mutators was contributed by Taylor Otwell. Laravel 9.x provides a new way to define Eloquent accessors and mutators. In Laravel 9.x you can specify an accessor and mutator utilizing a single, non-prefixed method by type-hinting a return type of Illuminate\Database\Eloquent\Casts\Attribute:

use Illuminate\Database\Eloquent\Casts\Attribute;

public function name(): Attribute
{
    return new Attribute(
        get: fn ($value) => strtoupper($value),
        set: fn ($value) => $value,
    );
}
Enter fullscreen mode Exit fullscreen mode

Enum Eloquent Attribute Casting

Eloquent now allows you to cast your attribute values to PHP enums. To accomplish this, you may specify the attribute and enum you wish to cast in your model's $casts property array:

use App\Enums\ServerStatus;

/**
 * The attributes that should be cast.
 *
 * @var array
 */
protected $casts = [
    'status' => ServerStatus::class,
];
Enter fullscreen mode Exit fullscreen mode

Once you have defined the cast on your model, the specified attribute will be automatically cast to and from an enum when you interact with the attribute:

if ($server->status == ServerStatus::provisioned) {
    $server->status = ServerStatus::ready;

    $server->save();
}
Enter fullscreen mode Exit fullscreen mode

Slot Name Shortcut

In previous releases of Laravel, slot names were provided using a name attribute on the x-slot tag:

<x-alert>
    <x-slot name="title">
        Server Error
    </x-slot>

    <strong>Whoops!</strong> Something went wrong!
</x-alert>
Enter fullscreen mode Exit fullscreen mode

However, beginning in Laravel 9.x, you may specify the slot's name using a convenient, shorter syntax:

<x-slot:title>
    Server Error
</x-slot>
Enter fullscreen mode Exit fullscreen mode

Controller Route Groups

Now you can utilize the controller method to define the common controller for all of the routes within the group. Then, when defining the routes, you only need to provide the controller method that they invoke:

use App\Http\Controllers\OrderController;

Route::controller(OrderController::class)->group(function () {
    Route::get('/orders/{id}', 'show');
    Route::post('/orders', 'store');
});
Enter fullscreen mode Exit fullscreen mode

Laravel Scout Database Engine

If your application interacts with small to medium-sized databases or has a light workload, Now you can use Scout's "database" engine instead of a dedicated search service such as Algolia or MeiliSerach. The database engine will use where like clauses and full-text indexes when filtering results from your existing database to determine the applicable search results for your query.

Improved Ignition Exception Page

Spatie Ignition open-source exception debug page has been redesigned from the ground up. The new, improved Ignition ships with Laravel 9.x and it includes light/dark themes, customizable "open in editor" functionality, and many more.

Implicit Route Bindings With Enums

PHP 8.1 introduces support for Enums. Laravel 9.x presents the ability to type-hint an Enum on your route definition and Laravel will only invoke the route if that route segment is a valid Enum value in the URI. Otherwise, an HTTP 404 response will be returned automatically. For example, given the following Enum:

enum Category: string
{
    case Fruits = 'fruits';
    case People = 'people';
}
Enter fullscreen mode Exit fullscreen mode

Now, You can define a route that will only be invoked if the {category} route segment is fruits or people. Otherwise, an HTTP 404 response will be returned:

Route::get('/categories/{category}', function (Category $category) {
    return $category->value;
});
Enter fullscreen mode Exit fullscreen mode

Above are some main features and updates of Laravel 9.x, If you want to explore more about Laravel 9, you can visit this link.

For More News & Updates, Follow me on Twitter/iamSumyyaKhan.

Top comments (0)