The Laravel team released v11.22 this week, with the chaperone() Eloquent method for inverse relations, support for passing backed Enums to Queuable methods, the ability to pass backed Enums to route ->name() and ->domain() methods, and more.
Chaperone Eloquent Relations
<iframe width=”1238″ height=”696″ src=”https://www.youtube.com/embed/bDa2X6_H0kU” title=”Laravel Chaperone Method” frameborder=”0″ allow=”accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share” referrerpolicy=”strict-origin-when-cross-origin” allowfullscreen></iframe>
Samuel Levy contributed the Eloquent inverse/chaperone model relation that Taylor demonstrated in his 2024 Larcon US keynote during the open-source part of the talk.
public function comments(): HasMany
{
return $this->hasMany(Comment::class)->chaperone(‘post’);
}
The chaperone()/inverse() method avoids unexpected N+1 queries by linking back to the parent after the relationship query has run. The above relationship will link the appropriate Post model in the correct relation on Comment instances (with scopes intact).
See Pull Request #51582 for more details.
Support Backed Enum in the Queueable Trait
Seth Phat contributed support for BackedEnum in Queuable trait methods:
onQueue()
onConnection()
allOnQueue()
allOnConnection()
Here’s an example from the pull request:
use AppEnumsQueueConnection;
use AppEnumsQueueName;
// Before
public function register()
{
$user = User::create([…]);
SendWelcomeEmailJob::dispatch($user)
->withConnection(QueueConnection::REDIS->value)
->onQueue(QueueName::HIGH->value);
}
// After
public function register()
{
$user = User::create([…]);
SendWelcomeEmailJob::dispatch($user)
->withConnection(QueueConnection::REDIS)
->onQueue(QueueName::HIGH);
}
Allow Enums to be Passed to Routes
@NickSdot contributed passing BackedEnum to Route domain() and name() methods:
// Before
Route::domain(InterfaceDomain::Marketing->value)
->name(Routes::Home->value)
->get(‘/contact’, ContactController::class);
// After
Route::domain(InterfaceDomain::Marketing)
->name(Routes::Home)
->get(‘/’contact, ContactController::class);
Release notes
You can see the complete list of new features and updates below and the diff between 11.21.0 and 11.22.0 on GitHub. The following release notes are directly from the changelog:
v11.22.0
[11.x] Fix FoundationServiceProvider docblock by @seriquynh in https://github.com/laravel/framework/pull/52542
[11.x] Fix ReflectionParameter @param type on Util::getContextualAttributeFromDependency() by @samsonasik in https://github.com/laravel/framework/pull/52541
[11.x] More specific parameter type in CastsInboundAttributes by @lorenzolosa in https://github.com/laravel/framework/pull/52536
[11.x] Unify prefetch API by @timacdonald in https://github.com/laravel/framework/pull/52550
[11.x] Add PDO subclass support for PHP 8.4 by @ju5t in https://github.com/laravel/framework/pull/52538
[11.x] Handle circular references in model serialization by @samlev in https://github.com/laravel/framework/pull/52461
[11.x] Eloquent inverse relations by @samlev in https://github.com/laravel/framework/pull/51582
[11.x] Feature/whereany closures by @liamduckett in https://github.com/laravel/framework/pull/52555
[11.x] Update remaining workflows to run on latest possible ubuntu version by @Jubeki in https://github.com/laravel/framework/pull/52566
Correct comments to better represent the updated method functionality by @dropweb in https://github.com/laravel/framework/pull/52564
[11.x] Support CSP nonce by @timacdonald in https://github.com/laravel/framework/pull/52558
[11.x] Allow enums to be passed to routes by @NickSdot in https://github.com/laravel/framework/pull/52561
[11.x] SORT_NATURAL on Collection no longer throws warning for nulls by @Chaplinski in https://github.com/laravel/framework/pull/52557
[11.x] Allow prefetch to start on custom event by @timacdonald in https://github.com/laravel/framework/pull/52574
[11.x] Fix regression in database assertions with custom model connections by @devfrey in https://github.com/laravel/framework/pull/52581
[11] Update DetectsLostConnections.php by @webartisan10 in https://github.com/laravel/framework/pull/52614
Fix docblock for Model::getEventDispatcher() by @inmula in https://github.com/laravel/framework/pull/52602
[11.x] Restore Request::HEADER_X_FORWARDED_PREFIX in TrustProxies by @taka-oyama in https://github.com/laravel/framework/pull/52598
[11.x] Accepts BackedEnum for onQueue, onConnection, allOnQueue, and allOnConnection methods in the Queueable trait by @sethsandaru in https://github.com/laravel/framework/pull/52604
[11.x] Use the same parameter type for ‘throwUnless’ as used for ‘throwIf’ by @pataar in https://github.com/laravel/framework/pull/52626
[11.x] Pass iterable keys to withProgressBar in InteractsWithIO by @robinmoisson in https://github.com/laravel/framework/pull/52623
[11.x] Fix docblock for Filesystem::hash() by @sunaoka in https://github.com/laravel/framework/pull/52630
Fix Apostrophe Handling in SeeInOrder.php and Enhance Test Coverage by @nomitoor in https://github.com/laravel/framework/pull/52627
[11.x] SQLite Error: “General error: 1 no such table” after adding a foreign key when using a table prefix. by @incrize in https://github.com/laravel/framework/pull/52578
The post Chaperone Eloquent Models in Laravel 11.22 appeared first on Laravel News.
Join the Laravel Newsletter to get all the latest
Laravel articles like this directly in your inbox.
Source: Read MoreÂ