Skip to content

Repeater field JSON preset could work with attributes nested inside an array but does not #6582

@k8n

Description

@k8n

The set() method of the Repeater fields' JSON preset works fine if the Repeater is pointed to a nested element of a model's json / array attribute, e.g. Repeater::make('Nested Items', 'json_attribute->items'). The get() method, however, does not.

I create my own preset to override the get() method as follows, and it all appears to work well. Perhaps this could be folded into Nova to give Repeater fields a bit more flexibility?

class NestedJSON extends JSON
{
    public function get(NovaRequest $request, Model $model, string $attribute, RepeatableCollection $repeatables)
    {
        return RepeatableCollection::make(
            data_get($model, Str::replace('->', '.', $attribute)) // as opposed to $model->{$attribute}
        )
            ->map(function ($block) use ($repeatables) {
                return $repeatables->newRepeatableByKey($block['type'], $block['fields']);
            });
    }
}

Thank you for considering this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions