It’s good news for the Laravel developers because Laravel released its new version Laravel 10 on 14th February 2023. Like always, Laravel comes up with cool and unique features with its new releases every year. Which makes our development more smooth and more straightforward in terms of functionalities. The updates of Laravel 10 make creating high-quality, scalable, and secure applications more manageable than ever by introducing several new features and improving existing functionality. Laravel 10 has something for everyone, whether you are a seasonal Laravel developer or just getting started. In this post, I will be discussing the important features of Laravel 10.
The Vision of Releasing Laravel 10
As you know Laravel and its first-party packages follow the Semantic Versioning methodology. It means that significant updates to the framework are released once a year in the first quarter mainly. The latest release (Laravel 10), comes up with new features and updates. If you look into the past, Laravel releases a new version every six months which is minor updates. Hence, as per the support policy, Laravel provides support for 18 months for bug fixes and two years for security patch updates.
The core team of the Laravel community used to release two significant variants every six months, once a year. But, the release schedule of Laravel was altered after an official statement by Laravel founder Taylor Otwell that a single mainstream variant would be released annually. Hence the core group and community needed to break down the framework to devote more time and effort to a particular technology version and incorporate new capabilities.
Now, let’s dive into some important features of Laravel 10.
Laravel 10 Drops Support of PHP 8.1
The new release of Laravel supports PHP >= 8.1 version. It won’t work with PHP 8.0 or older versions.
Interactive Command Line
Laravel 10 has an interactive command line for executing artisan commands. For example, if you hit any artisan command, it will prompt as shown below.
php artisan make:model
Here, I want to create a model. But, I haven’t given the model name that I want to create. However, the command line prompted for the model name to be created as shown below.
Now, if I give the model name then it will prompt again for creating some other related files like migration, seeder, factory, etc.
I have given the model name as Todo and as per the requirement, now, I am going to create all files together respective to this model. So, I will be selecting one of the options from the given list.
I have selected option 1 which means all. So, it will generate all files as shown above. You can create the file(s) selectively as per your need.
Now, we will move to the next amazing features of Laravel 10.
Native Type Declaration
Laravel 10 has introduced return type declaration which is called Native type declaration. In the previous versions of Laravel, we saw, Laravel utilized DocBlocks in its skeleton code to clearly explain the function of a particular code. But in the newest Type Declarations, it came up with additional features of type-hints and return types. Here, you can define the return type of the functions. These enhancements have additional benefits and do not impact the core framework’s backward compatibility. This includes Method Arguments, Return Types, elimination of unnecessary annotations when feasible, excluding Types property, and allowing users to access closure arguments.
Let’s understand through a sample code snippet of Laravel 10.
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
*/
class UserFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}
/**
* Indicate that the model's email address should be unverified.
*/
public function unverified(): static
{
return $this->state(fn (array $attributes) => [
'email_verified_at' => null,
]);
}
}
Take a look at the above snippet. This is the UserFactory class (default). In each function, you may see the return types are defined as an array and static. The return types will be the PHP data types.
Even the annotation for the functions has also been updated.
Laravel Pennant
The Laravel Pennant is a straightforward and lightweight feature flag package. This allows you to roll out new features within your application easily. The Laravel pennant includes an interface design for A/B testing. It sustains trunk-based development techniques and many more. The best thing about the feature flag is to turn a feature on or off at runtime without changing the code.
The Laravel core team itself manages this package for now. We will be using this package in our upcoming post. For more details, you can visit the official documentation.
Process Layer for Laravel
The Laravel Process service makes testing and running CLI processes a dream to work with. It offers a straightforward API to ease the burden of testing. Also, it enables you to run external processes in your Laravel application easily. This helps to address common use cases as well along with providing a highly improved development experience.
In order to use this, Laravel introduced new facades as shown below.
use Illuminate\Support\Facades\Process;
$result = Process::run('ls -la');
$result->successful();
$result->failed();
$result->exitCode();
$result->output();
$result->errorOutput();
$result->throw();
$result->throwIf($condition);
Now, move to the next features of Laravel 10.
Default Invokable Validation Rules
Laravel provides an option to create custom validation rules of their own. In Laravel 10 all the validation rules are invokable by default, which was not the case earlier.
In the previous version of Laravel (Laravel 9), you needed to add an invokable flag after the artisan command. However, Laravel 10 makes it default. Now, you don’t require the invokable flag in the command.
Let’s take a look at the below snippet.
# Laravel 9 flag to create an invokable and implicit rule
php artisan make:rule CustomValidation --invokable
php artisan make:rule CustomValidation --invokable --implicit
# Laravel 10 creates an invokable rule by default
php artisan make:rule CustomValidation
# Laravel 10 implicit rule
php artisan make:rule CustomValidation --implicit
Now, let’s try to create a rule in Laravel 10.
Dropped Out dispatchNow() Function
In Laravel 10, the dispatchNow() function no longer exists. It was a part of Laravel 9. However, instead of this, you can use another method which is dispatchSync(). The job execution process has become better because of dispatchSync() method in Laravel 10. It does not follow the queue process and dispatches the job immediately.
Task Scheduling
We can complete any large data processing in Laravel by scheduling a task and processing it later. There are several tasks in an application that are repetitive. In Laravel 10 one of the focuses was to optimize task scheduling which has been achieved with some upgrades. The console commands have improved to make it more efficient in terms of scheduling any tasks.
Dropped Support of Predis 1
Laravel 10 doesn’t support Predis 1 version anymore. Previously, in Laravel 9, Predis 1 and Predis 2 both were supporting. But, now, you will require to upgrade to Predis 2.
Routing
Apart from the above features, Laravel has worked on the optimization of Routing in Laravel 10. Optimizing the number of method calls has been reduced significantly along with property accessibility. Ultimately reducing the overhead and increasing the speed. In Laravel 10 route caching allows pre-compiling the routes into a single file. It helps in managing incoming requests efficiently.
whereExists() Method Support for Eloquent Builder
Currently, using whereExists()
requires configuring the nested query using a closure. Fortunately, with Laravel 10, it is now possible to include an Eloquent Builder as a nested query. It enables the usage of custom builder methods, model scopes, and so on.
If we want to use this method, generally, we write use whereExists()
:
Post::whereExists(function ($query) {
$query->from('comments')->whereColumn('posts.comment_id', 'comments.id');
});
However, in Laravel 10, it upgraded and it can be written as more simpler.
Order::whereExists(
Product::whereColumn('products.order_id', 'orders.id')
);
Upgrades in Official Packages
As per the framework updates in Laravel, it updated the packages also. So that they can support Laravel 10.
Here is the list of updated official packages
- Breeze
- Dusk
- Horizon
- Installer
- Jetstream
- Passport
- Pint
- Sail
- Valet
- Scout
- Cashier Stripe
Now, we will see which features have been deprecated from Laravel 10.
Deprecations from Laravel 10
In Laravel 10, a lot of features have been added. However, some of them have been deprecated as shown below
- Route::home method
- assertsTimesSent
- Native PHP 8.1 array_is_list function
- Dates property
- Composer 1. x is not supported anymore in Laravel 10.
That’s it for this post. I hope you find helpful to this post.
Leave a Reply