On 8th February 2022, Laravel 9 has been officially released. According to the official website. With this major version release, developers will get 1 year of bug fixes support and 2 years of security updates from the laravel team. Laravel 9 comes with various interesting features and overall improvement of the core framework. Let's find out what's new in Laravel 9.
At a glance Laravel 9
Version | Laravel 9 |
Release Date | 8th Feb 2022 |
Bug fixes | Until Feb 8, 2023 |
Security fixes | Until Feb 8, 2024 |
Minimum PHP | version 8 |
Laravel 9 required a minimum PHP version 8. New features are Controller route group, Accessors / Mutators improvement, Flysystem 3.x, Eloquent Enum casting, Inline blade templating, Full-text indexing, Enums implicit route binding, newly designed route listing, testing coverage option, new query builder interface, PHP 8 string function included, anonymous stub migrations, SwiftMailer to Symfony mailer and many interesting features. Let's discover these features.
From laravel 9 we can use common routes as controller group routes which will make route defining more easy and readable. Let's take a look at what it looks like.
use App\Http\Controllers\PostController;
Route::controller(PostController::class)->group(function () {
Route::get('/posts/{id}', 'show');
Route::post('/posts', 'store');
});
The server.php
file used for php artisan serve
. From laravel 9, it will be removed from the root of the project. This will be included from the inside of the framework.
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']);
In Laravel 9, the storage facade will use the Flysystem 3.
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
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();
});
}
};
From laravel 9, we can easily define eloquent model accessors and mutators every easily with sort syntax which is a very intuitive syntax.
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,
);
}
Like an as eloquent date, number and other types. Now we can cast eloquent attributes to Enum.
use App\Enums\PostStatus;
protected $casts = [
'status' => PostStatus::class,
];
In a table definition, we can use full-text indexes in laravel 9.
$table->text('bio')->fullText();
In query
DB::table('users')
->whereFullText('bio', 'developer')
->get();
Making checked box checked/unchecked, select box selected state. Now you can use the blade directive.
<input type="checkbox"
@checked(old('active', $user->active))
name="active"/>
<select name="user_id">
@foreach ($users as $row)
<option value="{{ $row->id }}" @selected(old('id') == $row->id)>
{{ $row->name }}
</option>
@endforeach
</select>
enum Category: string
{
case Fruits = 'fruits';
case People = 'people';
}
Route::get('/categories/{category}', function (Category $category) {
return $category->value;
});
Laravel 9 will be moved from SwiftMailer to Symfony Mailer. Here is the upgrade guide from SwiftMailer to Symfony Mailer.
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();
}
The \Illuminate\Support\Str
facade will use PHP 8 string function internally for str_contains
, str_starts_with
and str_ends_with
Helper
str('Laravel')->append(' Article');
// 'Laravel Article'
str()->snake('LaravelArticle');
//laravel_article
//HTTP redirect to a name route
return to_route('posts.show', ['post' => 1]);
Laravel 9 introduce a new query builder interface.
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
});
Laravel 9 comes with a beautiful route list design in CLI with colour highlighted HTTP verb. We'll see the defined route list with the command php artisan route:list
A new test coverage option was added in Laravel 9. You will get the test coverage by artisan commandartisan test --coverage
. Here is the sample output.
In this post, I have tried to cover all the highlighted Laravel 9 features as much as possible. Hope this post will let you learn about new laravel 9 functions and features. If you find this post helpful, please share it with others.