Exploring the Latest Features of Laravel 10.8
Apr 20, 2023, Originally published at techvblogs.com ・3 min read
This article provides a detailed overview of the latest release of Laravel, version 10.8. Readers will discover the new features and improvements that have been added, including enhanced performance, updated security features, and other key changes. The article also includes practical examples and insights into how these new features can be leveraged to improve development workflows and deliver better applications. Whether you’re a seasoned Laravel developer or just starting out, this article is a must-read for anyone looking to stay up-to-date with the latest developments in this popular PHP framework.
This week, the Laravel PHP Framework released version 10.8, which includes several new features, improvements, and syntactic enhancements to the Process::pipe()
methods. Also, class-based validation rules for "after" were added.
Let’s take a look at the new features and changes in Laravel 10.8.
Syntactic Sugar Added to Process::pipe()
Wendell Adriel contributed syntactic sugar to the pipe()
methods he contributed in Laravel 10:
/*
Given: the following in test.txt
Hello, world
foo
*/
// New syntatic sugar:
$result = Process::pipe([
'cat test.txt',
'grep -i "foo"',
]);
$result->output(); // "foo"
// Equivalent to the following, introduced in Laravel 10.7
$pipe = Process::pipe(function ($pipe) {
$pipe->command('cat test.txt'),
$pipe->command('grep -i "foo"'),
});
$pipe->run()->output(); // "foo"
Class-based “After” Validation Rules
Tim MacDonald contributed Class based on validation rules which allow for an array of “after” rules:
Validator::make(/* .. */)->after([
new AfterRuleOne(/* ... */),
new AfterRuleTwo(/* ... */),
function ($validator) use (/* ... */): void => {
// ...
},
});
Form requests can now be improved by adding an after()
function to your class:
class UserRequest
{
protected function rules(): array
{
return [
//
];
}
protected function after(MyService $service): array
{
return [
new AfterRule($this->value, $service),
// ...
];
}
}
Pull request #46757 to see the full details of what’s new with validation after rules in Laravel 10.8.
Allow specifying index name when calling constrained()
Luke Kuzmish added the ability to specify a name for an index when calling constrained()
// Before 10.8
$table->foreignIdFor(KnowledgeBaseResource::class)->nullable();
$table
->foreign('knowledge_base_resource_id', 'my_foreign_index')
->references('id')
->on('knowledge_base_resources')
->constrained()
->nullOnDelete();
Now, you can do the following instead:
$table->foreignIdFor(KnowledgeBaseResource::class)
->nullable()
->constrained(null, null, 'my_foreign_index')
->nullOnDelete();
Max Exceptions Added to Broadcasting an Event
Anjorin Damilare contributed a maxExceptions property you can include to events that determine the maximum number of unhandled exceptions you can allow before crashing. Refer to pull request #46800 for more details.
Release Notes
You can view the complete list of improvements and features in the following table. You can also see the differences between 10.7.0 to 10.8.0 on GitHub. This release note comes directly taken from the changelog:
v10.8.0
Added
- Added syntax sugar to the
Process::pipe
method (#46745) - Allow specifying index name when calling ForeignIdColumnDefinition@constrained() (#46746)
- Allow to customise redirect URL in AuthenticateSession Middleware (#46752)
- Added Class based after validation rules (#46757)
- Added max exceptions to broadcast event (#46800)
Fixed
- Fixed compiled view file ends with
.php
(#46755) - Fix validation rule names (#46768)
- ixed validateDecimal() (#46809)
Changed
- Add headers to exception in
Illuminate/Foundation/Application::abourd()
(#46780) - Minor skeleton slimming (framework edition) (#46786)
- Release lock for job implementing ShouldBeUnique that is dispatched afterResponse() (#46806)
Thank you for reading this article.