Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show how to cast attribute for Matrix field #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/en/docs/field.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down