Laravel 9 Tutorial: Laravel 9 new features

kaperskyguru

Solomon Eseme

Posted on July 27, 2021

Laravel 9 Tutorial: Laravel 9 new features

With the different features of Laravel, it is the most favorite framework amongst developers and companies using the PHP programming language. This adoption is due to the elegancy and scalability nature of the framework.

With the Laravel 9 release, the Laravel team switches from the traditional 6-month release cycle to a 12-month release cycle using the Semantic Versioning(Semver) approach.

The initial schedule of the Laravel 9 release was supposed to be September 2021, following the traditional approach. Still, the release date changed to January 2022 as stated by the creator and with a few valid reasons, as noted below.

In this article, we will discuss in detail Laravel 9 and the best features of Laravel. We will also detail how to update or upgrade to Laravel 9 and install and start building an app with the new Laravel 9.

Before you dive in, if you’re a backend developer or looking at delving into this career path, join other developers to receive daily articles on backend development that will boost your productivity.

What is Laravel?

Laravel is an open-source PHP web application framework with expressive, elegant syntax. It is an MVC framework for building simple to complex web applications using the PHP programming language.

Laravel strictly follows the MVC (Model-View-Controller) architectural pattern. It is known for its beautiful and elegant syntax as a web framework.

If you haven’t used Laravel, you can read through the ultimate guide to Laravel and peek at Laravel 8 new features to get started.

Laravel 9 and What to Expect

Laravel 9 is the first Long Term Support (LTS) released in a 12-month release cycle and was initially scheduled to be released by September 2021; however. The Laravel team decided to push it to January 2022.

This release schedule is due to these reasons outlined below: with these reasons below, the release date was pushed forward.

  1. Laravel as a framework relies on different community-driven and 9 Symfony libraries, and Symfony is planning to release v6.0 by November 2021, making the Laravel team delay release of Laravel v9.
  2. To update or upgrade the entire Laravel framework to the latest version of Symfony will take a while and also lots of testing and monitoring against any breaking changes before releasing it to the public.
  3. Finally, delaying the release of Laravel 9 till January 2022 will better position the Laravel team to release subsequent LTS yearly which gives the Laravel team 2 months after Symfony’s releases.

Now that we know the reasons for the changes, let’s explore the new Laravel v9 before it is released by January 2022.

Newest Features in Laravel 9

Below is the newest features and improvement that we should expect in the Laravel v9 by January 2022:

Minimum PHP Requirement

Laravel 9 requires the newest version of PHP 8, PHPUnit 9, and a few other requirements specified in the future.

The PHP 8 requirement is because Laravel 9 is solely dependant on Symfony’s latest v6.0 release, which depends on PHP 8.

You can explore what is new in PHP 8 and the different PHP versions benchmarks to grasp the improvements and features of PHP 8 from Just-In-Time compiler (JIT) to constructor property promotion.

Anonymous Stub Migration

The anonymous stub migration released in Laravel version 8.37 to solve this Github Issue will be the default behavior of the newest Laravel 9.

Anonymous Stub Migration eliminates the collision of migration class names. The collision happens when multiple migrations have the same class name, it’ll cause issues when trying to recreate the database from scratch.

Here is the sample code snippet of how the new migration file will look like:

<?php 

use Illuminate\Database\Migrations\Migration; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Support\Facades\Schema; 

 return new class extends Migration {

  /** * Run the migrations. 
  * 
  * @return void 
  */ 

 public function up() { 
   Schema::table('users', function (Blueprint $table) { 
     $table->string('first_name')->nullable(); 
   }); 
 } 

};
Enter fullscreen mode Exit fullscreen mode

This code snippet uses Schema::table instead of the traditional Schema::create() method.

New Query Builder Interface

Type hinting is crucial as developers highly rely on refactoring, static analysis, and code completion in their IDEs.

With Laravel 9, developers can now enjoy a shared interface or inheritance between Query\Builder, Eloquent\Builder and Eloquent\Relation with the new Query Builder Interface.

You can read through this for all the details:

<?php 

  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 
     });
Enter fullscreen mode Exit fullscreen mode

Laravel v9 added the new Illuminate\Contracts\Database\QueryBuilder interface and Illuminate\Database\Eloquent\Concerns\DecoratesQueryBuilder trait that will implement the interface in place of the existing __call magic method.

PHP 8 String Functions

Laravel v9 requires PHP v8, so the Laravel team agreed to merge a PR to use the newest PHP 8 String functions.

The suggested functions include str_contains(), str_starts_with() , and str_ends_with() to be used internally in the \Illuminate\Support\Str class.

And More

Laravel v9 is still in active development and will introduce more future improvements and features that we have not listed yet in this article. Laravel v9 will most definitely come with lots of significant changes, bug fixes, features, and of course, many breaking changes.

Do well to subscribe to our newsletter to stay ahead.

How to install Laravel 9

If you’re like us, we love to explore the newest technologies before being open to the public. You can start playing with the upcoming Laravel v9 for development and testing purposes. You can easily install and run it on your local machine following the guide below.

In addition, Laravel 9 supports PHP version 8. Make sure to check and upgrade your PHP version to use Laravel 9 before next January 2022.

To install Laravel 9, the first method is the Laravel global CLI which creates a new Laravel project from the branch specified. In this case, we will choose to create the new Laravel project from the dev branch.

Run the following command below in your directory to create a new Laravel 9 project. Make sure to have Laravel CLI installed globally.

laravel new laravel-9-todo-app --dev
Enter fullscreen mode Exit fullscreen mode

Next, to install Laravel 9 using composer, run the following command to install Laravel v9:

composer create-project --prefer-dist laravel/laravel laravel-9-todo-app dev-develop
Enter fullscreen mode Exit fullscreen mode

The composer command will create a new Laravel project with project name as laravel-9-todo-app, using the dev-develop will result in the latest Laravel 9.

Now that we have Laravel 9 installed, you can navigate to the new laravel-9-todo-app directory and run the following artisan command to check the version:

cd laravel-9-dev 
php artisan --version 

//Laravel Framework 9.x-dev
Enter fullscreen mode Exit fullscreen mode

Now that you have Laravel 9 installed, you should read the ultimate guide to Laravel to learn how to build scalable Laravel applications.

Conclusion

This article detailed the features of Laravel 9 and what to expect in future releases. It also explored how to update or upgrade to Laravel 9 and install and start building an app with the new Laravel 9.

Originally published at https://masteringbackend.com on July 20, 2021.


💖 💪 🙅 🚩
kaperskyguru
Solomon Eseme

Posted on July 27, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related