Laravel 9 released, See What's new.
Tags: laravel
Published: 2022-02-15 00:20:17

---
title: Laravel 9 released, See What's new.
published: true
date: 2022-02-09 13:13:05 UTC
tags: Laravel
canonical_url: https://coderflex.com/blog/laravel-9-released-see-what-s-new
---
# Introduction
Laravel 9 is finally here! It comes with a ton of handy new features, from routes to database, and this is what we are going to discover in this article.

![Laravel new design](https://i.imgur.com/kZrkzMs.png)

# Prerequisite
- Knowledge of PHP
- Basic Knowledge of Laravel
- PHP v8.0 at minimum

# Releases Schedule
For LTS releases, such as Laravel 9, bug fixes are provided for 2 years and security fixes are provided for 3 years. These releases provide the longest window of support and maintenance. For general releases, bug fixes are provided for 18 months and security fixes are provided for 2 years. For all additional libraries, including Lumen, only the latest release receives bug fixes. In addition, please review the database versions [supported by Laravel](https://laravel.com/docs/9.x/database#introduction).

| Version | PHP (*) | Release | Bug Fixes Until | Security Fixes Until |
| --- | --- | --- | --- | --- |
| 6 (LTS) (**) | 7.2 - 8.0 | September 3rd, 2019 | January 25th, 2022 | September 6th, 2022 |
| 7(-) | 7.2 - 8.0 | March 3rd, 2020 | October 6th, 2020 | March 3rd, 2021 |
| 8 | 7.3 - 8.1 | September 8th, 2020 | July 26th, 2022 | January 24th, 2023 |
| 9 (LTS) | 8.0 - 8.1 | February 8th, 2022 | February 8th, 2024 | February 8th, 2025 |
| 10 | 8.0 - 8.1 | February 7th, 2023 | August 7th, 2024 | February 7th, 2025 |


(-) End of life
(*) Supported PHP versions
(**) Security fixes only


# Laravel 9 new features

## New route:list look
when you type `php artisan route:list` you will see a brand-new __design__ look, besides you can see which middleware applied to each route!

![Route:list](https://i.imgur.com/1mwTfFx.png)

## Route Groups

You may now use the `controller` method to define the common controller for all the routes within the group. Then, when defining the routes, you only need to provide the controller method that they invoke:

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

## Forced Scoped Bindings
 you may now instruct Laravel to scope "child" bindings even when a custom key is not provided. To do so, you may invoke the `scopeBindings` method when defining your route:

```php
use App\Models\Post;
use App\Models\User;
 
Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) {
    return $post;
})->scopeBindings();
```
Or, you may instruct an entire group of `route` definitions to use scoped bindings:

```php
Route::scopeBindings()->group(function () {
    Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) {
        return $post;
    });
});
```

If you want to know more, you may take a look [here](https://laravel.com/docs/9.x/releases#forced-scoping-of-route-bindings)

## Blade Template Inline Rendering
Sometimes you may need to transform a raw Blade template string into valid HTML. You may accomplish this using the `render` method provided by the `Blade` facade. The `render` method accepts the Blade template string and an optional array of data to provide to the template:

```php
use Illuminate\Support\Facades\Blade;
 
return Blade::render('Hello, {{ $name }}', ['name' => 'Oussama Sid']);
```

Similarly, the `renderComponent` method may be used to render a given class component by passing the component instance to the method:

```php
use App\View\Components\HelloComponent;
 
return Blade::renderComponent(new HelloComponent('Oussama Sid'));
```

## Optional Bootstrap 5 Pagination Views
Laravel now includes pagination views built using [Bootstrap 5](https://getbootstrap.com/). To use these views instead of the default Tailwind views, you may call the paginator's `useBootstrapFive` method within the boot method of your `App\Providers\AppServiceProvider` class:

```php
use Illuminate\Pagination\Paginator;
 
/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Paginator::useBootstrapFive();
}
```


## Brand new Ignition Page
Ignition, the open source exception debug page created by [Spatie](https://spatie.be/), has been redesigned from the ground up. The new, improved Ignition ships with Laravel `9.x` and includes __light__ / __dark__ themes, customizable __"open in editor"__ functionality, and more.

![Brand new Ignition Page](https://user-images.githubusercontent.com/483853/149235404-f7caba56-ebdf-499e-9883-cac5d5610369.png)

## str() helper function
The `str` function returns a new `Illuminate\Support\Stringable` instance for the given string. This function is equivalent to the `Str::of` method:

```php
$string = str('Oussama')->append(' Sid');

// Oussama Sid
```

If no argument is provided to the str function, the function returns an instance of `Illuminate\Support\Str`:

```php

$string = str()->append('Oussama Sid')->uppercase();

// OUSSAMA SID

```

## to_route() helper function
The `to_route` function generates a redirect HTTP response for a given named route, providing an expressive way to redirect to named routes from your routes and controllers:

```php
return to_route('users.show', ['user' => 1]);

// equivalent to

return redirect()->route('user.show', ['user' => 1]);

```

## Enum Attribute Casting

> Enum casting is only available for __PHP 8.1+__

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:

- Create `PostStatus` class inside `app/Enums` directory

```php
namespace App\Enums\PostStatus;

enum PostStatus
{
    case Published;
    case Pending;
    case Archived;
    case Draft;
}

```

Inside your model:

```php
use App\Enums\PostStatus;
 
/**
 * The attributes that should be cast.
 *
 * @var array
 */
protected $casts = [
    'status' => PostStatus::class,
];
```
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:

```php
if ($post->status == PostStatus::Pending) {

    $post->status = PostStatus::Published;

    $post->save();
}
```
## Brand-new Accessors & Mutators (Simplified)
__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:

```php
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`:

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

# Laravel 9 Release Notes
__Laravel 9__ comes with release notes, and the full list of __features__, you can see them on [Laravel 9 release notes](https://laravel.com/docs/9.x/releases#laravel-9)

# Upgrade To Laravel 9
You have 2 possible ways to upgrade your __application__ to __Laravel 9__
- First one is to do it manually, [Check The Upgrade Guide](https://laravel.com/docs/9.x/upgrade)
- Use Automation tools such as [Laravel Shift](https://laravelshift.com/)

# Conclusion
The features above are just a few ones on  this new release, if you want to know more, you may refer to __Laravel 9 release notes__

If you are a visual learner, you may check [Laravel 9 - Everything You Need to Know (In 45 Minutes)
](https://www.youtube.com/watch?v=GwunZ5sH2P8) by Jeffery way on laracasts, or [12 Minutes, What's New in Laravel 9
](https://www.youtube.com/watch?v=MnSzUX7kU-E) by Mohamed Said.

This all what I want to say today, Have a happy __Upgrade__ 🥳