Laravel is known as the most popular PHP framework. Laravel delivers enjoyable and creative experience to the Laravel developers for the most web projects.
However, there is always a room for the improvements. Previously, we have shared the expected Laravel 9 features. On February 8, 2022; Laravel has rolled out the new long-term support version of Laravel 9 with new features and fascinating enhancements. The new features include a minimum PHP v8.0 version, controller route groups, a refreshed default ignition error page, Symfony mailer integration, Improved Eloquent mutators/accessors, scout database, Flysystem 3.x, and many more.
With the release of Laravel 9, Laravel community has announced that, from now, Laravel will release a new major version about every year instead of six-month schedule. It is intended to easy the maintenance burden on the community and challenge Laravel team to deliver great new enhancements without any breaks. So, the next major version of Laravel 10 is scheduled to be release at February 7th, 2023.
Moreover, Laravel team has announced that Laravel 9 is the next long-term support and receive bug fixes until February 2024 and security fixes until 2025.
In this article, Explore 9.x top features, enhancements, release notes and improvements.
YOU MAY ALSO LIKE: Laravel 10 is coming soon!
Minimum PHP Requirement
Laravel 9 will be using the upgraded version of Symfony v6.0, so Laravel 9.x requires a minimum PHP version 8.0.2
Symfony Mailer
The Swift Mailer library was used by the preceding versions of Laravel. But it is no longer maintained; and Symfony mailer has taken the palace of it. So, your Laravel developer team needs to upgrade your Laravel application to be compatible with Symfony mailer.
Where Clauses and Full text indexes
Laravel 9 allows full-text indexes to column definitions when using MySQL or PostgreSQL.
Full-text indexes:
$table->text('role')->fullText();
whereFullText( ) method:
$users = DB::table('users') ->whereFullText('role', 'admin') ->get();
Scout Database Engine
You can use Scout database engine if your custom Laravel application requires small to medium sized databases. It will use "where like" clauses and full text indexes when you require to filter results from existing database.
Flysystem 3.x
While upgrading to Laravel 9, ensure your application must be compatible with Flysystem 3.x. As Laravel 9 upgrades their upstream Flysystem dependency to Flysystem 3.x.
Laravel 9 upgrades Flysystem dependency to Flysystem 3.x. It powers all file system interactions by the ‘Storage’ facade.
Laravel Breeze API & Next.js
With the upgrade of Laravel 9, The Laravel Breeze starter kit has obtained scaffolding mode of API and Next.js frontage execution. This may utilize to start your custom applications which are currently presenting as a backend.
Controller Route Group Improvements
Laravel 9 allows you to use controller method to define the common controller for all the routes within the group.
use App\Http\Controllers\ProductController; Route::controller(ProductController::class)->group(function () { Route::get('/product/{slug}', 'show'); Route::post('/product', 'store');});
Scoped Route Bindings
Laravel experts may wish to extend the next Eloquent model in a route way that must be a childe of the previous Eloquent model. For example, you may need to retrieves a blog post for a specific user by slug:
use App\Models\Post;use App\Models\User; Route::get('/users/{user}/posts/{post:slug}', function (User $user, Post $post) { return $post;});
Laravel will automatically extend the query to get the settled model by its parent using conventions to guess the correlation name on the parent. Although, this behavior was only supported by Laravel when a custom key was used for the child route binding. In Laravel 9.x, developers need to call on Laravel to scope “child” bindings whenever custom key is not provided. It will require to call on the scopeBindings method whenever route definition is required:
use App\Models\Post;use App\Models\User; Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) { return $post;})->scopeBindings();
Or it requires to instruct an entire group of route definitions to use scoped bindings:
Route::scopeBindings()->group(function () { Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) { return $post; });});
Laravel 9 New Designs
Welcome the New Enhanced Ignition Exception Page
The new Laravel 9 error page has been revamped from the scratch. It is including added information, functionalities like “open in editor,” dark / light themes, stack, context, and routes parameters and more. So, it can help developers to understand better.
Beautiful Experience of Route:list
The Route:list command line interface output has been improved for the Laravel 9 release, and it is offering an enhanced experience when executing route definitions.
New test ‘—coverage’ option in the terminal
In Laravel 9, ‘artisan test –coverage option has been introduced. It will display the test coverage directly on the terminal.
It has been come with ‘—min’ option; to specify minimum threshold for meeting the test coverage percentage.
Laravel’s New Helpers
New ‘str()’ and ‘to _route()’ helper functions
str( )
The new helper in Laravel 9 is str( ). The str function will return a new Stringable instance.
$string = str('Adam')->append(' Mathew');// 'Adam Mathew' $snake = str()->snake('OldRecord');// 'old_record'
Developers are already known to this; if no argument is passed, the str function returns an instance of Illuminate\Support\Str.
to_route( )
The to_route function will use for redirecting user to a specific route from your routes and controllers:
return to_route('users.show', ['user' => 1]);
If require, you can also append the HTTP status code for the redirection as following:
return to_route('users.show', ['user' => 1], 302, ['X-Status' => 'Redirected']);
Enums in Laravel 9
In this article, we are also going to cover some of the most important utilizations of Laravel Enums:
Route Bindings with Enums (PHP 8.1)
PHP 8.1 launches the support for Enums. Laravel 9.x has the capability to type-hint on Enum on your route definition; it will only invoke if that route segment is valid; otherwise, it will return HTTP 404 response. For e.g.:
enum Category: string{ case Cloths = 'shirt'; case Electronic = 'television';}
Enum Eloquent Attribute Casting
Eloquent permits you to cast your attribute values to PHP Enums. Enum casting is only supported for PHP 8.1+. Laravel developers just need to use $casts property array:
use App\Enums\ServerStatus; /** * The attributes that should be cast. * * @var array */protected $casts = [ 'status' => ServerStatus::class,];
Once it defined; the attribute will be cast to and from an Enum:
if ($server->status == ServerStatus::provisioned) { $server->status = ServerStatus::ready; $server->save();}
Other Laravel 9.x Release Notes:
Enhanced Eloquent Accessors/Mutators
With the release of Laravel 9, it provides an enhanced way to define Eloquent accessors and mutators; using a non-prefixed and single method by type-hinting a return type of
use Illuminate\Database\Eloquent\Casts\Attribute; public function name(): Attribute{ return new Attribute( get: fn ($value) => strtoupper($value), set: fn ($value) => $value, );}
Cache object values is also a new way to define accessories; just like custom cast classes.
use App\Support\Address;use Illuminate\Database\Eloquent\Casts\Attribute; public function address(): Attribute{ return new Attribute( get: fn ($value, $attributes) => new Address( $attributes['address_line_first'], $attributes['address_line_second'], ), set: fn (Address $value) => [ 'address_line_first' => $value->lineFirst, 'address_line_second' => $value->lineSecond, ], );}
Slot name Shortcut
The latest version of Laravel 9.x needs to clarify the slot’s name with a shorter syntax:
<x-slot:title> Server Error</x-slot>
Pagination Views with Bootstrap 5
The pagination views developed using Bootstrap 5 are now available in Laravel. It may need to call the paginator’s useBootstrapFive method within the boot method of your App\Providers\AppServiceProvider
class:
use Illuminate\Pagination\Paginator; /** * Bootstrap any application services. * * @return void */public function boot(){ Paginator::useBootstrapFive();}
Upgraded IDE Support Collections
Laravel 9.x is now available with upgraded “generic” style definitions to the collections component, by enhancing static analysis and IDE support. Laravel collections are now better understood by IDEs such as PHPStorm and PHPStan.
Inline Blade Templates Rendering
To transform raw Blade Template string into valid HTML, use the “render” method provided by the “Blade” facade. It will accept the Blade template string and optional array of the data.
use Illuminate\Support\Facades\Blade; return Blade::render('Hello, {{ $name }}', ['name' => 'Adam Mathew']);
Likely, the renderCommnad will be used to render a given class components by calling the component instance to the method:
use App\View\Components\HelloComponent; return Blade::renderComponent(new HelloComponent(' Adam Mathew '));
Checked / Selected Blade Directives
Laravel developers can use the @checked directive to indicate if a given HTML checkbox input is “checked” or not. It will echo checked if the condition asses the true:
<input type="checkbox" name="active" value="active" @checked(old('active', $user->active)) />
Similarly, the @selected
directive is used to indicate a given select option to be “selected”:
<select name="version"> @foreach ($product->versions as $version) <option value="{{ $version }}" @selected(old('version') == $version)> {{ $version }} </option> @endforeach</select>
Upgraded Validation of Nested Array Data
Developer can now get the value for given nested array element when assigning validation rules to the attribute with using the Rule::forEach method. It accepts a closure which will be invoked for each looping of the array under validation and gets the value and explicit with a completely extended attribute name.
use App\Rules\HasPermission;use Illuminate\Support\Facades\Validator;use Illuminate\Validation\Rule; $validator = Validator::make($request->all(), [ 'companies.*.id' => Rule::forEach(function ($value, $attribute) { return [ Rule::exists(Company::class, 'id'), new HasPermission('manage-company', $value), ]; }),]);
The Laravel 9.x is the global team effort of their contributors and the Laravel community is always appreciating their efforts. We hope this article gives you a sneak peek at the new features and major improvements of the Laravel 9.x LTS Release!
So, what are you waiting for? Upgrade your Laravel application to the Laravel 9.x. Skynet Technologies is specialized in Laravel development services including custom development, upgrades, migration, Laravel enterprise solution, restful API development, extension development, patch upgrades, third-party integration services, e-commerce development, custom Laravel design services, and much more. Get in touch with us at [email protected] to know more or submit the following request form to upgrade Laravel 9.x.
Source: Laravel 9 Release Notes