3

I'm using the repeater field on my resource which looks and works great for editing, but how do I display the json data it saves to my detail page? I assume I use the KeyValue field for displaying but how do I access the json data correctly within that field? The Repeater fields saves json like:

[
    {
        "type": "location-contact",
        "fields": {
            "name": "mike",
            "email": "[email protected]"
        }
    },
    {
        "type": "location-contact",
        "fields": {
            "name": "john",
            "email": "[email protected]"
        }
    },
    {
        "type": "location-contact",
        "fields": {
            "name": "Mary",
            "email": "[email protected]"
        }
    }
]

However the KeyValue field expects the json to look like:

{
    "name": "mike",
    "email": "[email protected]"
}

How do I access the json data in the first example (since that's how the Repeater field dumps it in) using either the Repeater or KeyValue field? The docs on the Repeater field mention nothing of the detail view, so I'm assuming we use the KeyValue field.

Repeater::make('Location Contacts', 'location_contacts')
          ->repeatables([
              LocationContact::make(),
            ])->asJson(),
// Is there a way this field can display json data on detail view?

or

KeyValue::make('Location Contacts', 'location_contacts'), 
// Do I use ->withMeta to display the json data on detail view?
2
  • 👀 did you ever find a solution @mcornille ? I'm up against the same challenge myself at the moment. Commented Apr 23, 2024 at 12:46
  • I ended up using the boot method's created and updated to save the values to another resource altogether. Then they can be viewed nicely in a table by the using the HasMany relationship. I removed all the actions on the resource so its only for viewing. Commented Jul 18, 2024 at 15:49

2 Answers 2

1

I had a similar case and displayed my data using an additional Text field. With your example repeater, it looks like:

Repeater::make('Location Contacts', 'location_contacts')
          ->repeatables([
              LocationContact::make(),
            ])->asJson(),
Text::make('Location Contacts', 'location_contacts')
          ->onlyOnDetail()
          ->displayUsing(fn (array $value) => $value['fields']['name']),

It's non-ideal but does the job.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm able to achieve this using a nova package Laravel Nova Flexible Content


Flexible::make('Location Contacts', 'location_contacts')
            ->button('Add Contact')
            ->addLayout('Contact', 'wysiwyg', [
                 Text::make('Name'),
                 Text::make('Email'),
              ]),


This shows the repeater fields in Add/Edit/Detail views.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.