Laravel’s popularity has been immense, majorly because of its functionality, clarity, and simplicity. The power of Laravel is such that it can customize any web application using faultless syntax and the best development techniques. It decreases development time and increases coding productivity.
What’s so Great about Laravel Framework?
Laravel is the only framework capable of handling everything from database management to HTML generation with ease. Laravel comes equipped with a comprehensive full-stack model that would be of great assistance to developers. That is why it kept updating its software every 6 months with a few features.
For instance, Laravel 8’s numerous powerful features included enhanced HTTP client support, enhanced Breeze starter kits, parallel testing support, and even new Eloquent relationship types like “has one of many.” These brilliant features were launched without compromising compatibility with previous versions.
About Laravel 9 Version
Laravel began releasing new versions annually with the release of its 9th edition. In the past, substantial updates on the Laravel framework occurred every 6 months. The company’s goal in making this move was to only reduce the amount of work required to keep the developers up and running without compromising existing functionality. For this sole reason, Laravel 9’s “major” release was for “maintenance,” like upgrading upstream dependencies. Let’s dig more into the features of this latest version.
New Features of Laravel 9
-
PHP 8 prerequisite
Laravel employs Symfony 6, which necessitates a minimum of PHP 8. PHP 8 includes a new just-in-time (JIT) compiler, the OPcache extension, and named arguments, among other enhancements.
-
Symphony Mailer succeeded Swift Mailer
Swift Mailer, which has been used for years in Laravel, is being withdrawn and will no longer be supported. In Laravel v9 and subsequent editions, Symfony Mailer will be required. Check out the upgrade guide if you’re updating an existing Laravel instance.
-
Enhanced Eloquent accessors and mutators
With the syntax Illuminate\Database\Eloquent\Casts\Attribute, you can now define a model prefix with a singular non-prefixed phrase in Laravel 9. With a single-method approach, you can now retrieve and modify attributes.
Example of Illuminate\Database\Eloquent\Casts\Attribute;
public function username(): Attribute
{
return new Attribute(
get: fn ($value) => strtoupper($value),
set: fn ($value) => $value,
);
}
-
Usage of ‘Where’ clauses and full-text indexing
If your Laravel application uses MySQL or PostgreSQL, you can now utilize the fulltext feature on column definitions in migration files to construct full-text indexes.
Example of fulltext and where clauses:
$table->text(‘content’)->fullText();
$laravelPosts= DB::table(‘post’)
->whereFullText(‘content’, ‘laravel’)
->get();
-
Controller route groups
The controller function of the Laravel 9 Route class may now be used to define the controller that will be used for each route in a route group.
Example of App\Http\Controllers\PostController;
Route::controller(PostController::class)->group(function () {
Route::get(‘/post/{id}’, ‘show’);
Route::post(‘/post’, ‘store’);
});
-
Introducing a new database engine for Scout
Laravel 9 includes the Laravel Scout database engine that adds full-text search abilities to Eloquent models. This engine will utilize “where-like” clauses to filter database results. Also, this feature will keep search indexes properly synced with Eloquent records. Moreover, it is suitable for web applications with even small-sized databases and moderate programming.
Example of Laravel\Scout\Searchable to a model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Article extends Model
{
use Searchable;
}
-
Breeze API using Next.js
Laravel 9 has an amazing feature of the Breeze starting package that provides a complementary Next.js frontend implementation. With this starter kit scaffolding, you will have the capability to create Laravel applications with Laravel Sanctum authentication that act as both a backend and a JavaScript frontend.
-
Inline Blade rendering
Laravel 9’s version offers inline Blade rendering that now makes it possible to convert a raw Blade template into valid HTML.
Example of Illuminate\Support\Facades\Blade:
return Blade::render(‘Hello, {{ $name }}’, [‘name’ => ‘Andrew Adab’]);
-
Enums feature for implicit route bindings
In Laravel route definitions, you can now specify hints using a PHP enum. Laravel will then invoke the route if the route’s URI contains a valid enum. It will return a 404 only if one of the enums cannot be located.
Example of Enums:
enum vegetable: string
{
case Spinach = ‘spinach’;
case Bottle Gourd = ‘bottle gourd’;
}
This particular route will only be invoked if the {vegetable} route matches one of the enums.
Route::get(‘/vegetables/{vegetable}’, function (Vegetable $vegetable return $vegetable->value;
});
-
Query builder Interface
In the previous versions of Laravel, it was difficult for developers to resolve a TypeError when they worked with QueryBuilder, EloquentBuilder, or EloquentRelation. This new query builder interface enables the entry of Eloquent queries with hints.
Example of 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
});
-
Defining route scope bindings
Using conventions to infer the relationship name of the parent, Laravel 9 can now automatically scope the query to fetch a nested model by its parent in a route declaration.
Example of scope bindings:
use App\Models\Article;
use App\Models\User;
Route::get(‘/users/{user}/articles/{article}’, function (User $user, Article $article) {
return $article;
})->scopeBindings();
You can also use scope bindings on a group of route definitions.
use App\Models\Article;
use App\Models\User;
Route::get(‘/users/{user}/articles/{article}’, function (User $user, Article $article) {
return $article;
})->scopeBindings();
-
Bootstrap 5 pagination views
If you have ever attempted to develop your own pagination code, you are aware that it is not easy. This new version of Laravel makes it simple to integrate Bootstrap 5 pagination views into the pages of your application. All you have to do is include the Illuminate\Pagination\Paginator and invoke its useBootstrapFive method in the boot method of the App\Providers\AppServiceProvider class for your application.
Example of Illuminate\Pagination\Paginator;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Paginator::useBootstrapFive();
}
-
Extended help in String Functions
Since Laravel is using PHP 8, it will make full use of its string functions. Its \Illuminate\Support\Str will use PHP 8 string functions, which come with some new methods, including str_contains, str_ends_with, and str_starts_with. This extended help includes append and snake.
Example of string functions:
$string = str(‘Bob’)->append(‘ Smith’); // ‘Bob Smith’
$snake = str()->snake(‘LaravelIsGreat’); // ‘laravel_is_great’
-
Route Name String function
Besides offering many easy ways to create routes in Laravel, another feature that was added to Laravel 9’s string functions is the to_route function. This function creates a redirect HTTP response for a named route. You can use it to redirect to named routes from controllers and routes using: return to_route(‘posts.show’, [‘post’ => 25]);
-
Blade directives of checked or selected
You must be wondering, “How to set a checkbox checked in Laravel.” Laravel 9 has found a solution to this question. Now you can use the @checked directive to optimize a checkbox as ‘checked.’ If it evaluates to true, it will echo checked.
Example of checked/selected checkbox:
<input type= “checkbox”
name= “optin”
value= “optin”
@checked(old(‘optin’, $user->optin)) />
-
More stringent validation of nested array data
The Illuminate\Validation\Rule validation class now includes a forEach function that accepts a closure to execute ‘forEach’ array attribute iteration. The closure returns an array of rules to assign to each element of the array.
Example of Illuminate\Validation\Rule:
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),
];
}),
]);
-
Soketi echo server
Laravel now includes the Soketi echo server, a Node.js-based Laravel Echo-compatible WebSocket server. For developers who wish to run their own WebSocket servers, it is an open-source alternative to Ably and Pusher.
-
Unidentified stub migration
The anonymous stub migration behavior is now the default when performing a Laravel migration. This functionality was accessible in Laravel 8.3, but it is now standard in Laravel 9. This functionality prevents name clashes with migration classes. Before this modification, it was difficult to recreate a database from scratch when a class name was reused. You no longer need to worry about this.
-
Enhanced exception web pages
In addition, the exception page in Laravel 9 has been overhauled from the ground up. There are both bright and dark themes available, as well as an “open in editor” option.
-
Breakdown of routes
Laravel already had the route:list Artisan command, but it now provides a more detailed, color-coded breakdown of the routes in your application.
-
Test coverage
The Artisan test command now has a –coverage option, which will output the test coverage percentages in the CLI output.
-
Flysystem 3.x
In Laravel v9, Flysystem was migrated from v1.x to v3.x. Flysystem handles all the file manipulation functions that the Storage facade provides. Some changes you will see are:
- The put, write, and writeStream methods now overwrite existing files by default
- The put, write, and writeStream methods no longer throw an exception on a write error
- If you wish to read a file that doesn’t exist, null will be returned
- Deleting a file that doesn’t exist now returns to its original form
Simplest Way to Install Laravel 9
For development and testing purposes, it is simple to install and execute Laravel 9 locally. As stated previously, Laravel only supports the PHP 8 engine version. Verify your PHP version prior to installation and testing. To simplify issues, remove the current version and install a new one. Here is the installation guide the company has documented.
To install, run:
composer create-project –prefer-dist laravel/laravel laravel-9-dev test-laravel9
If you have already installed the Laravel Installer as a global composer dependency, use the following:
laravel new laravel-9-dev test-laravel9
After you finish installing Laravel 9, enter the new directory, and run the artisan command to check the current version.
cd dev-laravel9
php artisan –version
Conclusion
Laravel has proven time and again to be the best PHP framework. With its “amazing” new features already available, Laravel 9 is attracting developers’ interest more. Laravel 9 is the first version to follow a 12-month release cycle because it took its time to resolve many glitches of the previous version. It has come back with a great list of features that will make programming all the more exciting and easier.