Map Eloquent Attributes into an Object Using the Collection Cast in Laravel 12.10

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 as
Collection::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.