From dbb9dea2c5fc40b2c1cb81c6626b9c6c7a88c568 Mon Sep 17 00:00:00 2001 From: Karam Qubsi Date: Wed, 7 Sep 2022 12:44:02 +0800 Subject: [PATCH] Show how to cast attribute for Matrix field --- docs/en/docs/field.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/en/docs/field.md b/docs/en/docs/field.md index 9a1605c2b..aa4fd4fdc 100644 --- a/docs/en/docs/field.md +++ b/docs/en/docs/field.md @@ -604,6 +604,21 @@ Matrix::make('users') ]), ``` +Data in DB will be stored in JSON format, and once retrived must be casted as an associative array, you can do something like this in your model to do the job: + +```php +use Illuminate\Database\Eloquent\Casts\Attribute; +... + protected function users(): Attribute + { + return Attribute::make( + get: fn ($value) => json_decode($value, true), + set: fn ($value) => json_encode($value), + ); + } + +``` + > **Note.** The matrix under the hood does the copying of the fields on the client side. This works fine for simple `input/select/etc` fields, but may not work well for complex or compound fields. ## Code editor