Map Eloquent Attributes into an Object Using the Collection Cast in Laravel 12.10
Last updated on by Paul Redmond
Never Miss a Laravel Release 🚀
The Laravel team released v12.10.0, which includes mapping Eloquent attributes using the collection cast into a specific object, checking nested relationships with relationLoaded(), Arr::dot() performance improvements, and more:
Map Eloquent Attributes into other Objects
@DarkGhostHunter contributed the ability to map an Eloquent attribute into a given class using the AsCollection::of() method:
use App\ValueObjects\Option;use Illuminate\Database\Eloquent\Casts\AsCollection; protected function casts(): array{ return [ 'options' => AsCollection::of(Option::class) ];} // Same asCollection::make($this->attributes['options'])->mapInto(Option::class);
These objects should implement Laravel's Arrayable contract and PHP's JsonSerializable interface. See the Eloquent documentation for more details.
Add the Conditionable Trait to Fluent
Michael Nabil contributed the Conditionable trait to the Fluent support class, providing an expressive way to conditionally modify values in a fluent instance:
// Before$data = Fluent::make([ 'name' => 'Michael Nabil', 'developer' => true, 'posts' => 25,]); if (auth()->isAdmin()) { $data = $data->set('role', 'admin');} else { $data = $data->forget('posts');} // After$data = Fluent::make([ 'name' => 'Michael Nabil', 'developer' => true, 'posts' => 25,])->when(auth()->isAdmin(), function (Fluent $input) { return $input->set('role', 'admin');})->unless(auth()->isAdmin(), function (Fluent $input) { return $input->forget('posts');});
Improved Arr::dot() Performance
@cyppe contributed performance improvements to the Arr::dot() method with improvements of speed up to 150-300x on large arrays:
This PR optimizes the Arr::dot() method, replacing the recursive implementation with a closure-based iterative approach. The optimization significantly improves performance when flattening large nested arrays, which is particularly beneficial for Laravel's validator when processing large datasets.
See Pull Request #55495 for details and an explanation of this update.
Check Nested Relations with relationLoaded()
Before Laravel 12.10, the relationLoaded() method only checks single-level relations of a model. In this release, Mahesh Perera contributed the ability to check nested relations with the relationLoaded() method:
$user->load('posts.comments'); // Previously$user->relationLoaded('posts'); // true$user->relationLoaded('posts.comments'); // false // Now$user->relationLoaded('posts'); // true$user->relationLoaded('posts.comments'); // true
Release notes
You can see the complete list of new features and updates below and the diff between 12.9.0 and 12.10.0 on GitHub. The following release notes are directly from the changelog:
v12.10.0
- Use value() helper in 'when' method by @mohammadrasoulasghari in https://github.com/laravel/framework/pull/55465
- [12.x] Test
@usedirective without quotes by @osbre in https://github.com/laravel/framework/pull/55462 - [12.x] Enhance Broadcast Events Test Coverage by @roshandelpoor in https://github.com/laravel/framework/pull/55458
- [12.x] Add
ConditionableTrait toFluentby @michaelnabil230 in https://github.com/laravel/framework/pull/55455 - [12.x] Fix relation auto loading with manually set relations by @patrickweh in https://github.com/laravel/framework/pull/55452
- Add missing types to RateLimiter by @ClaudioEyzaguirre in https://github.com/laravel/framework/pull/55445
- [12.x] Fix for global autoload relationships not working in certain cases by @litvinchuk in https://github.com/laravel/framework/pull/55443
- [12.x] Fix adding
setTagsmethod on new cache flush events by @erikn69 in https://github.com/laravel/framework/pull/55405 - Fix: Unique lock not being released after transaction rollback in ShouldBeUnique jobs with afterCommit() by @toshitsuna-otsuka in https://github.com/laravel/framework/pull/55420
- [12.x] Extends
AsCollectionto map items into objects or other values by @DarkGhostHunter in https://github.com/laravel/framework/pull/55383 - [12.x] Fix group imports in Blade
@usedirective by @osbre in https://github.com/laravel/framework/pull/55461 - chore(tests): align test names with idiomatic naming style by @kauffinger in https://github.com/laravel/framework/pull/55496
- Update compiled views only if they actually changed by @pizkaz in https://github.com/laravel/framework/pull/55450
- Improve performance of Arr::dot method - 300x in some cases by @cyppe in https://github.com/laravel/framework/pull/55495
- [12.x] Add tests for
CacheBasedSessionHandlerby @imanghafoori1 in https://github.com/laravel/framework/pull/55487 - [12.x] Add tests for
FileSessionHandlerby @imanghafoori1 in https://github.com/laravel/framework/pull/55484 - [12.x] Add tests for
DatabaseSessionHandlerby @imanghafoori1 in https://github.com/laravel/framework/pull/55485 - [12.x] Fix many to many detach without IDs broken with custom pivot class by @amir9480 in https://github.com/laravel/framework/pull/55490
- [12.x] Support nested relations on
relationLoadedmethod by @tmsperera in https://github.com/laravel/framework/pull/55471 - Bugfix for Cache::memo()->many() returning the wrong value with an integer key type by @bmckay959 in https://github.com/laravel/framework/pull/55503
- [12.x] Allow Container to build
Migratorfrom class name by @cosmastech in https://github.com/laravel/framework/pull/55501