You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Adds `AsColection::map()` section
As part of [#55383](laravel/framework#55383).
* Added more clarification
* More clarification
* Update eloquent-mutators.md
Co-authored-by: Sebastian Hädrich <[email protected]>
* Adds openinh PHP on full class
* Better, almost final, clarification.
* Minor change to the Option example so it makes sense
* Better clarification about serialization
* formatting
---------
Co-authored-by: Sebastian Hädrich <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
Copy file name to clipboardExpand all lines: eloquent-mutators.md
+68
Original file line number
Diff line number
Diff line change
@@ -449,6 +449,74 @@ protected function casts(): array
449
449
}
450
450
```
451
451
452
+
The `of` method may be used to indicate collection items should be mapped into a given class via the collection's [`mapInto` method](/docs/{{version}}/collections#method-mapinto):
453
+
454
+
```php
455
+
use App\ValueObjects\Option;
456
+
use Illuminate\Database\Eloquent\Casts\AsCollection;
457
+
458
+
/**
459
+
* Get the attributes that should be cast.
460
+
*
461
+
* @return array<string,string>
462
+
*/
463
+
protected function casts(): array
464
+
{
465
+
return [
466
+
'options' => AsCollection::of(Option::class)
467
+
];
468
+
}
469
+
```
470
+
471
+
When mapping collections to objects, the object should implement the `Illuminate\Contracts\Support\Arrayable` and `JsonSerializable` interfaces to define how their instances should be serialized into the database as JSON:
472
+
473
+
```php
474
+
<?php
475
+
476
+
namespace App\ValueObjects;
477
+
478
+
use Illuminate\Contracts\Support\Arrayable;
479
+
use JsonSerilizable;
480
+
481
+
class Option implements Arrayable, JsonSerializable
0 commit comments