laravel / nova-issues

556 stars 34 forks source link

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

Open k8n opened 5 days ago

k8n commented 5 days ago

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.

crynobone commented 4 days ago

Unable to reproduce the issue, please provide full reproducing repository based on fresh installation as suggested in the bug report template (or you can refer to https://github.com/nova-issues for example)