Laravel, the leading PHP web application framework, has just released version 11.30, packed with exciting new features and improvements boosting developer productivity and code flexibility. Let's explore the key updates.
Streamlined Testing with New Helpers: withDefer()
and withoutDefer()
Laravel 11.30 introduces withDefer()
and withoutDefer()
, courtesy of Tim MacDonald. These are invaluable when testing code utilizing deferred calls, allowing you to selectively disable deferral for precise assertion control.
Illustrative example using withoutDefer()
:
<code class="language-php">// This won't work correctly User::create(/* ... */); $this->assertAgainstSomeDeferredOutcome(); // This works as intended $this->withoutDefer(); User::create(/* ... */); $this->assertAgainstSomeDeferredOutcome();</code>
This enhanced control over deferred operations during testing leads to more reliable and accurate test results.
Custom Unique String IDs with the HasUniqueStringIds
Trait
Luke Kuzmish's contribution, the HasUniqueStringIds
trait, empowers developers to utilize custom unique string IDs as route keys without modifying resolveRouteBindingQuery()
. This expands on the existing HasUuids
and HasUlid
traits, offering greater ID generation flexibility.
Implementing custom unique string IDs:
<code class="language-php">trait HasTwrnsTrait { use HasUniqueStringIds; public function newUniqueId() { return (string) Twrn::new(); } protected function isValidKey($value): bool { return Twrn::isValid($value); } }</code>
This feature allows for highly customized model identifiers while seamlessly integrating with standard Laravel practices.
Enhanced Authorization with Direct Enum Support
Johan van Helden's update to the AuthorizesRequests
trait now directly accepts backed enums, aligning with Laravel's recent embrace of Enums framework-wide.
Using an Enum with the authorize()
method:
<code class="language-php">enum DashboardPermission: string { case VIEW = 'dashboard.view'; } // Previously public function index(): Response { $this->authorize(DashboardPermission::VIEW->value); // ... } // Now public function index(): Response { $this->authorize(DashboardPermission::VIEW); // ... }</code>
This simplification improves type safety and readability in authorization code.
Other Significant Enhancements
Version 11.30 also includes:
Blade::directive
functionality with a $bind
parameter.trans_choice()
issues with translation replacements containing the |
separator.exists()
instead of count()
in specific scenarios.PostTooLargeException
.failed_jobs_uuid_unique
.Laravel 11.30 underscores the framework's dedication to continuous improvement and developer experience. With refined testing, flexible model IDs, and streamlined Enum-based authorization, this release provides developers with powerful new tools for crafting cleaner, more efficient code. Laravel remains a top choice for PHP developers building robust and feature-rich web applications.
Ready to leverage Laravel to elevate your business? Contact us today.
The above is the detailed content of Laravel A Leap Forward in Testing, Model IDs, and Authorization. For more information, please follow other related articles on the PHP Chinese website!