Laravel 9 continues the improvements made in Laravel 8.x by introducing support for Symfony 6.0, Symfony Mailer, Flysystem 3.0 components, improved route:list
output, Laravel Scout database driver , Eloquent accessor / mutator syntax, binding via Enums and many other types of bug fixes and improvements.
PHP Version
Laravel 9 requires PHP version 8.0 – 8.1 and Symfony 6.0
Controller route groups
You can use the controller method of Laravel 9 Route class
1 2 3 4 5 6 7 | use AppHttpControllersPostController; Route::controller(PostController::class)->group(function () { Route::get('/post/{id}', 'show'); Route::post('/post', 'store'); }); |
Better accessors and mutators in Eloquent
In Laravel 9, you can use Illuminate Database Eloquent Casts Attribute
to declare a prefix. Using a method call, you can now both get and set properties.
1 2 3 4 5 6 7 8 9 10 | use IlluminateDatabaseEloquentCastsAttribute; public function username(): Attribute { return new Attribute( get: fn ($value) => strtoupper($value), set: fn ($value) => $value, ); } |
Fulltext indexes and where clauses
If you are using MySQL
or PostgreSQL
in your Laravel application, you can now use the fulltext
method on the column definition in your migration
to create a full-text index.
1 2 | $table->text('content')->fullText(); |
You can then use the whereFullText
and orWhereFullText
to add to your query.
1 2 3 4 | $laravelPosts = DB::table('post') ->whereFullText('content', 'laravel') ->get(); |
The new Scout database engine
Laravel v9 comes out with the new Laravel Scout database engine. It provides full-text search for Eloquent models. This tool will use “where-like” clauses when filtering results from your database. To use you just add to your model
: LaravelScoutSearchable
1 2 3 4 5 6 7 8 9 10 11 | <?php namespace AppModels; use IlluminateDatabaseEloquentModel; use LaravelScoutSearchable; class Article extends Model { use Searchable; } |
Breeze API with Next.js
Laravel v9 includes a Next.js UI implementation in Breeze. Using this framework, you can build Laravel applications that act as backends and JavaScript frontends using Laravel Sanctum authentication.
Inline Blade rendering
If you need to convert a Blade template to HTML, you can now do it with Blade inline.
1 2 3 4 | use IlluminateSupportFacadesBlade; return Blade::render('Hello, {{ $name }}', ['name' => 'Stephan Miller']); |
New query builder interface
The new query builder interface in Eloquent makes it possible to import Eloquent suggested queries. In the past, it was difficult to tell if you were dealing with , or for developers to guess what to fix whenever a TypeError popped up.
1 2 3 4 5 6 7 8 9 10 11 | return Model::query() ->whereNotExists(function($query) { // $query is a QueryBuilder }) ->whereHas('relation', function($query) { // $query is an EloquentBuilder }) ->with('relation', function($query) { // $query is an EloquentRelation }); |
Implicit route bindings with enums
You can now enter hints with a PHP enum in your Laravel. Then Laravel will only invoke the variable in the enum that is valid in the URI and will return a 404 if one of the enums is not found.
1 2 3 4 5 6 | enum Fruit: string { case Apple = 'apple'; case Cherry = 'cherry'; } |
Use in router
1 2 3 4 | Route::get('/fruits/{fruit}', function (Fruit $fruit) { return $fruit->value; }); |
Forced scope route bindings
Laravel 9 can dynamically scope queries to retrieve a model nested by its parent using conventions to guess inheritance relationship names. For example:
1 2 3 4 5 6 7 | use AppModelsArticle; use AppModelsUser; Route::get('/users/{user}/articles/{article}', function (User $user, Article $article) { return $article; })->scopeBindings(); |
You can also use scope constraints
1 2 3 4 5 6 7 | use AppModelsArticle; use AppModelsUser; Route::get('/users/{user}/articles/{article}', function (User $user, Article $article) { return $article; })->scopeBindings(); |
Bootstrap 5 pagination views
Instead of writing your own pagination you can paginate with pagination views in Bootstrap 5.
1 2 3 4 5 6 7 8 9 10 11 12 | use IlluminatePaginationPaginator; /** * Bootstrap any application services. * * @return void */ public function boot() { Paginator::useBootstrapFive(); } |
New helpers
Laravel 9 is using PHP 8, IlluminateSupportStr
its facade will use PHP 8 string functions, which comes with several new methods, including str_contains
, str_starts_with
and str_ends_with
. New helpers include append
and snake
.