Laravel 9.0 Released : All You Need To Know About Laravel 9

March 11, 2022

Last Updated: March 11, 2022

Laravel has been one of the most popular PHP frameworks for many years. It is adaptable, scalable, versatile, and has become one of the in fact the systems for engineers and companies working with PHP.

Laravel v9 released on 8 February 2022, and it offers plenty of new features.

From the releases support policy documentation, for all Laravel releases, bug fixes are provided for 18 months and security fixes are provided for 2 years. It means Laravel 9 will receive bug fixes until February 2023 and security fixes until February 2024.

Before we jump into the detailed new features, we are going to point out that with the launch of Laravel 9, Laravel will release its new versions about every 12 months instead of the 6 months schedule.

New Features in Laravel 9

Now, let’s explore the list of the features and improvements you should expect in the upcoming major release of Laravel.

Hetarth Consulting is a leading software development company that provides excellent web app development services utilising cutting-edge technologies. We help startups and enterprises to raise their business value by offering innovative software products. If you have a business idea, then feel free to connect with us anytime.

PHP 8 the minimum version in Laravel 9

Laravel 9 development will require Symfony 6.0, which has a minimum requirement of PHP 8 hence Laravel 9 will be restricted in the same way. Laravel 9.x requires a minimum PHP version of 8.0.

PHP 8 String Functions

Since Laravel 9 targets PHP 8, Laravel combined this PR, suggesting utilising the most up to date PHP 8 string functions. These functions incorporate the use of str_contains(), str_starts_with(), and str_ends_with() internally in the IlluminateSupportStr class.

Symfony Mailer

Symfony deprecated Swift Mailer and Laravel 9 makes the change to use Symfony Mailer for mail transport. There are a few breaking changes as a result, you can see the PR for more information.

Improved route:list CLI Output

The route:list CLI output has been significantly improved for the Laravel 9.x release, offering a beautiful new experience when exploring your route definitions.

route-list-laravel9

New Test Coverage

A new test coverage option was added in Laravel 9. You will get the test coverage by artisan command.

artisan test --coverage

Here is the sample output.

test-coverage-laravel9

Improved Eloquent Accessors / Mutators

Laravel 9.x offers a new way to define Eloquent accessors and mutators.

Before Laravel 9

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

In Laravel 9

use Illuminate\Database\Eloquent\Casts\Attribute;
public function name(): Attribute{
    return new Attribute(
        get: fn ($value) => strtoupper($value),
        set: fn ($value) => $value,
    );
}

Enum Eloquent Attribute Casting

Like an eloquent date, number and other types. Now we can cast eloquent attributes to Enum.

use App\Enums\PostStatus;
protected $casts = [
    'status' => PostStatus::class,
];

Implicit Route Bindings With Enums

enum Category: string{
    case Fruits = 'fruits';
    case People = 'people';
}
Route::get('/categories/{category}', function (Category $category) {
    return $category->value;
});

Anonymous Stub Migrations

To prevent migration class name collisions we can use anonymous stub migration features in laravel 9. Laravel 9 will use this feature as default for the command.

php artisan make:migration

Before this change, it was difficult to re-create a database from scratch if you reused a class name. Now, you won’t have to worry about this.

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
    public function up(){
        Schema::table('people', function (Blueprint $table) {
            $table->string('name')->nullable();
        });
    }
};

Controller route groups

You can now use the controller method of the Laravel 9 Route class to define the controller that will be used for every route in a route group.

use App\Http\Controllers\PostController;
Route::controller(PostController::class)->group(function () {
    Route::get('/post/{id}', 'show');
    Route::post('/post', 'store');
});

New Query Builder Interface

Thanks to Chris Morrell, Laravel 9 will highlight a new Query Builder Interface, and you can view this merged PR for all the details.

For developers who depend on sort insights for static analysis, refactoring, or code completion in their IDE, the need for a shared interface or legacy between Query\Builder, Eloquent\Builder, and Eloquent\Relation can be very complex.

return Model::query()
    ->whereNotExists(function($query) {
        // $query is a Query\Builder
    })
    ->whereHas('relation', function($query) {
        // $query is an Eloquent\Builder
    })
    ->with('relation', function($query) {
        // $query is an Eloquent\Relation
    });

This version added the new Illuminate\Contracts\Database\QueryBuilder interface, as well as the Illuminate\Database\Eloquent\Concerns\DecoratesQueryBuilder a trait that will implement the interface in place of the __call magic method.

Breeze API with Next.js

Laravel v9 includes a complimentary Next.js frontend implementation in its Breeze starter kit. By using this starter kit scaffolding, you can build Laravel applications that serve as both a backend and a JavaScript frontend using Laravel Sanctum authentication.

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.

Inline Blade Templating

With laravel 9, we can convert any blade template string to valid HTML tags easily with the help of an inline blade template. Here an example is given below.

use Illuminate\Support\Facades\Blade;

return Blade::render('Hi, {{$name}}', ['name' => 'Jhon']);

Soketi Echo Server

Although not exclusive to Laravel 9.x, Laravel has recently assisted with the documentation of Soketi, a Laravel Echo compatible Web Socket server written for Node.js. Soketi provides a great, open source alternative to Pusher and Ably for those applications that prefer to manage their own Web Socket server.

Full Text Indexes / Where Clauses

$table->text('bio')->fullText();
DB::table('users')
    ->whereFullText('bio', 'developer')
    ->get();

Laravel Scout Database Engine

If your application interacts with small to medium sized databases or has a light workload, you may now use Scout’s “database” engine instead of a dedicated search service such as Algolia or MeiliSearch.

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.

Pagination – Bootstrap 5

Laravel 9 included bootstrap 5 pagination view. To use bootstrap 5 pagination view add this line in your app service provider inside the boot method.

use Illuminate\Pagination\Paginator;
public function boot(){
    Paginator::useBootstrapFive();
}

References:

https://laravel.com/docs/master/releases

Looking for Outsourcing Partner for Laravel 9 Application Development?

We are a Laravel app development company having some of the best developers in the industry. Our Laravel app development services come with intuitive engagement models for our client’s best experience.

Let’s be in touch!!

A Seasoned full stack IT development and management professional with 14+ years of rich experience in driving business requirements analysis, solution design consulting, business intelligence and software services & products.

Follow on : Linkedin | YouTube

Wondering how to build
an exclusive software product for your business?

Discuss your project needs with our architects.