Install laravel 9 and new features with examples

Install laravel 9 and new features with examples
Spread the love

You know already that, the laravel 9 has been released in February 2022. In this new version, laravel adds a couple of features. Today’s lesson I am going to show how to install the new version as well as new features. Laravel v9 was scheduled to be released around September of this year, but the Laravel Team decided to push this release back to February of 2022.

We know that Laravel uses a variety of community-driven packages as well as nine Symfony components for a number of features within the framework. Symfony 6.0 is due for release in November. For that reason, we are choosing to delay the Laravel 9.0 release until 2022.


By delaying the release, we can upgrade our underlying Symfony components to Symfony 6.0 without being forced to wait until September 2022 to perform this upgrade. In addition, this better positions us for future releases as our yearly releases will always take place two months after Symfony’s releases.

Install laravel v9

To install the new version of laravel, you can install via composer as well as using laravel global installer.

composer create-project --prefer-dist laravel/laravel nine

or

laravel new nine --dev

Well, after successfully installing laravel you can see the laravel version using this command.

php artisan --version

// output
Laravel Framework 9.x-dev

Well, now serve your application by this command. php artisan serve then copy the URL and hit your favorite browser.

Read More : Laravel Application Deploy in Heroku Cloud Platform

laravel 9

Well, we have successfully installed the new version on laravel. Now I will show you the new features of this latest version.

New Design for routes:list

Using this command (php artisan route:list) we can see the available route in our application in the terminal. Sometimes it looks messy when our application has a lot of routes. Let’s see the new look of the console.

Anonymous Stub Migrations

Earlier this year, Laravel 8.37 came out with a new feature called Anonymous Migrations that prevents migration class name collisions. When Laravel 9 launches, this will be the default when you run php artisan make:migration . Let’s create a migration file then see the change.

php artisan make:migration create_admins_table

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('admins');
    }
};

Well, you can see an anonymous class file has been created for migration.

New Query Builder Interface

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

For developers who rely on type hints for static analysis, refactoring, or code completion in their IDE, the lack of a shared interface or inheritance between Query\BuilderEloquent\Builder and Eloquent\Relation can be pretty tricky:

 1return Model::query() 2	->whereNotExists(function($query) { 3		// $query is a Query\Builder 4	}) 5	->whereHas('relation', function($query) { 6		// $query is an Eloquent\Builder 7	}) 8	->with('relation', function($query) { 9		// $query is an Eloquent\Relation10	});

This feature adds a new Illuminate\Contracts\Database\QueryBuilder interface and a Illuminate\Database\Eloquent\Concerns\DecoratesQueryBuilder  the trait that implements the interface in place of the existing __call implementation.

PHP 8 String Functions

In this version added three new PHP8 string functions they are str_contains()str_starts_with() and str_ends_with() functions internally in the \Illuminate\Support\Str class.

From SwiftMailer to Symfony Mailer

Previous releases of Laravel utilized the Swift Mailer library to send outgoing emails. However, that library is no longer maintained and has been succeeded by Symfony Mailer.

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

Laravel 9. x offers a new way to define Eloquent accessors and mutators. In previous releases of Laravel, the only way to define accessors and mutators was by defining prefixed methods on your model like so:

    public function getNameAttribute($value)
    {
        return strtoupper($value);
    }

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

However, in Laravel 9. x you may define an accessor and mutator using 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,
        );
    }

Implicit Route Bindings With Enums

PHP 8.1 introduces support for Enums. Laravel 9. x introduces 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 Users: string
{
    case Shahin = 'shahin';
    case Omi = 'omi';
}

You may define a route that will only be invoked if the {name} route segment is users or people. Otherwise, an HTTP 404 response will be returned:

Route::get('user/{name}',function(Users $name){
    return $name->value;
});

Controller Route Groups

You may now use 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:


Route::controller(DemoController::class)->group(function(){
    Route::get('demo', 'index')->name('demo');
    Route::post('demo', 'store')->name('demo');
});

Spread the love

About Anisur Rahman Shahin

Hello. My name is Shahin. I'm a tech enthusiast guy. Personally, I’m an Optimistic and always in hurry kinda person. I'm a freelance web developer. I am working at Zakir Soft as Laravel Developer. My Portfolio website: https://tutspack.com

View all posts by Anisur Rahman Shahin →

Leave a Reply

Your email address will not be published.