Logo

dev-resources.site

for different kinds of informations.

Laravel 11.30: A Leap Forward in Testing, Model IDs, and Authorization

Published at
1/13/2025
Categories
laravel
webdev
releases
php
Author
Luigi Laezza
Categories
4 categories in total
laravel
open
webdev
open
releases
open
php
open
Laravel 11.30: A Leap Forward in Testing, Model IDs, and Authorization

Laravel, the popular PHP web application framework, has recently unveiled its latest release, version 11.30. This update brings a host of new features and improvements that enhance developer productivity and code flexibility. Let's dive into the key highlights of this release.

New Testing Helpers: withDefer() and withoutDefer()

One of the standout features in Laravel 11.30 is the introduction of new testing helpers, withDefer() and withoutDefer(), contributed by Tim MacDonald. These helpers are particularly useful when working with tests that use defer, but you need to disable it to assert the outcome of a deferred call.

Here's an example of how to use the new withoutDefer() helper:

// This will not work
User::create(/* ... */);
$this->assertAgainstSomeDeferredOutcome();

// This will work
$this->withoutDefer();
User::create(/* ... */);
$this->assertAgainstSomeDeferredOutcome();

These helpers provide developers with more control over deferred operations during testing, allowing for more precise assertions and improved test reliability.

Custom Unique String IDs with HasUniqueStringIds Trait

Luke Kuzmish has introduced the HasUniqueStringIds trait, which allows developers to use custom unique string IDs as route keys without overriding the resolveRouteBindingQuery() method. This update builds upon the existing HasUuids and HasUlid eloquent traits, providing more flexibility in ID generation.

Here's an example of how to implement custom unique string IDs:

trait HasTwrnsTrait
{
    use HasUniqueStringIds;

    public function newUniqueId()
    {
        return (string) Twrn::new();
    }

    protected function isValidKey($value): bool
    {
        return Twrn::isValid($value);
    }
}

This feature allows for greater customization of model identifiers while maintaining compatibility with existing Laravel conventions.

Enhanced Authorization with Enum Support

Johan van Helden has updated the AuthorizesRequests trait to accept backed enums directly. This change aligns with Laravel's recent updates to support direct use of Enums in various parts of the framework.

Here's how you can now use an Enum with the authorize() method:

enum DashboardPermission: string
{
    case VIEW = 'dashboard.view';
}

// Before
public function index(): Response
{
    $this->authorize(DashboardPermission::VIEW->value);
    // ...
}

// After
public function index(): Response
{
    $this->authorize(DashboardPermission::VIEW);
    // ...
}

This enhancement simplifies authorization code and improves type safety when working with permission enums.

Other Notable Improvements

Laravel 11.30 also includes several other enhancements:

  • Added a $bind parameter to Blade::directive for improved blade directive functionality.
  • Fixed trans_choice() when translation replacements include the | separator.
  • Improved performance by using exists() instead of count() in certain operations.
  • Added support for custom Postgres operators.
  • Introduced optional dimensions for the vector column type.
  • Provided an error message for PostTooLargeException.
  • Fixed an integrity constraint violation on failed_jobs_uuid_unique.

Laravel 11.30 demonstrates the framework's commitment to continuous improvement and developer-friendly features. With enhanced testing capabilities, more flexible model ID options, and streamlined authorization using Enums, this release offers developers new tools to write cleaner, more efficient code. As Laravel continues to evolve, it remains a top choice for PHP developers seeking a robust and feature-rich web application framework.

Interested on how I can help you elevate your business with Laravel?

Get in touch with me.

Featured ones: