在 Laravel Nova 的详细信息视图上显示模型的 Repeater 字段 JSON 数据

问题描述 投票:0回答:1

我正在资源上使用中继器字段,该字段看起来非常适合编辑,但如何显示它保存到详细信息页面的 json 数据?我假设我使用 KeyValue 字段进行显示,但如何在该字段中正确访问 json 数据? Repeater 字段保存 json,如下所示:

    {
        "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]"
        }
    }
]

但是 KeyValue 字段期望 json 如下所示:

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

如何使用 Repeater 或 KeyValue 字段访问第一个示例中的 json 数据(因为这是 Repeater 字段转储数据的方式)? Repeater 字段上的文档没有提及详细视图,因此我假设我们使用 KeyValue 字段。

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

KeyValue::make('Location Contacts', 'location_contacts'), 
// Do I use ->withMeta to display the json data on detail view?
php json laravel laravel-nova
1个回答
0
投票

我有一个类似的案例,并使用附加的

Text
字段显示我的数据。使用您的示例中继器,它看起来像:

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

虽然不理想,但可以完成工作。

© www.soinside.com 2019 - 2024. All rights reserved.