Skip to content

Commit 34f6140

Browse files
Adds AsColection::map() section
As part of [#55383](laravel/framework#55383).
1 parent 9f8f242 commit 34f6140

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

eloquent-mutators.md

+20
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,26 @@ protected function casts(): array
449449
}
450450
```
451451

452+
If you desire the collection items to be mapped into an specific class instance, or pass each of them to a callable, you may use a second parameter, or the `map()` method if you want to use the base Collection class.
453+
454+
```php
455+
use App\ValueObjects\Option;
456+
use Illuminate\Database\Eloquent\Casts\AsCollection;
457+
use Illuminate\Support\Fluent;
458+
459+
/**
460+
* Get the attributes that should be cast.
461+
*
462+
* @return array<string, string>
463+
*/
464+
protected function casts(): array
465+
{
466+
return [
467+
'options' => AsCollection::map(Option::class)
468+
];
469+
}
470+
```
471+
452472
<a name="date-casting"></a>
453473
### Date Casting
454474

0 commit comments

Comments
 (0)