diff --git a/eloquent-mutators.md b/eloquent-mutators.md index 7aad935399..f0bd9c8d67 100644 --- a/eloquent-mutators.md +++ b/eloquent-mutators.md @@ -449,6 +449,74 @@ protected function casts(): array } ``` +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): + +```php +use App\ValueObjects\Option; +use Illuminate\Database\Eloquent\Casts\AsCollection; + +/** + * Get the attributes that should be cast. + * + * @return array + */ +protected function casts(): array +{ + return [ + 'options' => AsCollection::of(Option::class) + ]; +} +``` + +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: + +```php + $this->name, + 'value' => $this->value, + 'is_locked' => $this->isLocked, + ]; + } + + /** + * Specify the data which should be serialized to JSON. + * + * @return array{name: string, data: string, is_locked: bool} + */ + public function jsonSerialize(): array + { + return $this->toArray(); + } +} +``` + ### Date Casting