What’s New in Laravel 9? Let’s dive into it!

Check this article on Medium: @ngodingbang/whats-new-in-laravel-9-let-s-dive-into-it-3971954f3f62

You may want to read this article in Bahasa Indonesia version: Laravel 9 Rilis! Apa Saja yang Baru di Laravel 9?

TL;DR

Laravel has just released their latest version in version 9 on February 8, 2022. The interesting part about this version is now they force us to use PHP 8 (specifically 8.0.2 or above), which is very different with the previous Laravel version that still allows us to use PHP 7.2. And if we look at the image below, Laravel 9 is still supported for the next 1 year until Laravel 10 is released on February 7, 2023.

Support Policy of Laravel 6 until Laravel 10 (https://laravel.com/docs/9.x/releases#support-policy)
Support Policy of Laravel 6 until Laravel 10 (https://laravel.com/docs/9.x/releases#support-policy)

This Laravel version actually doesn’t have any significant features compared with Laravel 8. Otherwise, they introduce many features without removing their features from the previous version. So for those of you who are used in Laravel 7 or 8, you shouldn’t have to adapt too much when using version 9. So here are the new features of Laravel 9 that I’ve been summarized from their official documentation here, check it out!

If in previous Laravel versions we could only create accessors/mutators by making each method as the code shown below:

Defining accessor/mutator in the previous Laravel version

<?php

use Illuminate\Database\Eloquent\Model;

class MyModel extends Model
{
    public function getNameAttribute($value)
    {
        return strtoupper($value);
    }

    public function setNameAttribute($value)
    {
        $this->attributes['name'] = $value;
    }
}

Now we can simply create a method that returns a return of type Illuminate\Database\Eloquent\Casts\Attribute as the code shown below:

How to define accessor/mutator in Laravel 9

<?php

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;

class MyModel extends Model
{
    public function name(): Attribute
    {
        return new Attribute(
            get: fn ($value) => strtoupper($value),
            set: fn ($value) => $value,
        );
    }
}

Now we can use a method provided by the Route facade called controller() when declaring a route group that share the same controller class as shown below.

Controller Route Groups di Laravel 9

<?php

use App\Http\Controllers\OrderController;
use Illuminate\Support\Facades\Route;

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

If they use the facade/ignition library in their last default error page, now in Laravel version 9 it has been completely replaced by spatie/laravel-ignition library. Ignition is an open-source library from Spatie that maintains the debug page for Laravel version 9 which is utilized with many features such as light/dark themes, a customizable “open in editor”, etc.

Latest Ignition page in Laravel 9 using spatie/laravel-ignition (Source: https://laravel.com/docs/9.x/releases#exception-page)
Latest Ignition page in Laravel 9 using spatie/laravel-ignition (Source: https://laravel.com/docs/9.x/releases#exception-page)

Maybe there are so many of you that experiencing difficulty in exploring your Laravel routes using the php artisan route:list command. But now they rework the output of route:list so that we can see it in a more beautiful way rather than their previous version.

A new outlook of Laravel 9 route list (Source: https://laravel.com/docs/9.x/releases#improved-route-list)
A new outlook of Laravel 9 route list (Source: https://laravel.com/docs/9.x/releases#improved-route-list)

Conclusion

This is only a little from so many improvements they’ve made in this new Laravel version, yet actually there are still many new features that I don’t mention here. So for those of you who want to see further what changes Laravel has brought in this new version, you can visit the official Laravel documentation at https://laravel.com/docs/9.x/releases.


Source

Related Content