Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display the Model's Repeater Field JSON Data on the Detail View in Laravel Nova

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?
like image 795
mcornille Avatar asked Jan 20 '26 12:01

mcornille


1 Answers

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.

like image 200
Ralitsa Vladimirova Avatar answered Jan 22 '26 02:01

Ralitsa Vladimirova



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!