thyyppa/nova-json

Nova json 字段,用于将 json 列分散到多个字段中。

资助包维护!
naoray

安装: 637

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 20

类型:

v4.0.0 2023-02-08 20:43 UTC

This package is auto-updated.

Last update: 2024-09-09 00:18:09 UTC


README

Software License Total Downloads Tests

JSON 字段包装器允许您指定多个字段,这些字段将被解析为单个模型属性。这允许您单独验证存储在 json 列中的每条信息。

JSON::make('Author', [
    Text::make('Name')->rules(['string', 'required', 'min:3']),
    Text::make('Email')->rules(['email', 'required']),
])

上述内容将解析为模型上的单个 author 属性。

// prequesite: the 'author' attribute needs to casted into a json castable type
// e.g. object, array, ...
['author' => ['name' => '', 'email' => '']]

安装与设置

composer require naoray/nova-json

将您想在 JSON 字段中使用的列名添加到资源的模型上的 $casts 数组中!

用法

您可以将一个 JSON 列解构为多个 Nova 字段,并为每个键值对应用唯一的规则。

use Naoray\NovaJson\JSON;

// within your nova resource
public function fields()
{
    return [
        //...
        JSON::make('Some Json Column Name', [
            Text::make('First Field'),
            Text::make('Second Field'),
            Text::make('Third Field'),
        ]);
    ]
}

FillUsing 回调

->fillUsing() 回调通常用于直接填充模型的属性。使用此包时,不需要填充模型的属性,而是应该返回您想要在模型本身上保存的值。

JSON::make('Address', 'address', [
    Text::make('Street')->fillUsing(fn ($request, $model, $attribute, $requestAttribute) => $request[$requestAttribute] . ' Foo'),
]);

上面的例子可能有些愚蠢,但它是为了演示这个概念。_ Foo_ 值将被附加到 nova 中每个 address->street 值。

一次性填充

当使用数据传输对象(与可序列化 dto 配合良好)时,您不希望每个字段单独填充,因为那样 dto 的验证就毫无意义了。使用 fillAtOnce() 方法将添加一个 Hidden 字段,避免单独填充单个字段。相反,所有值将一次性通过 Hidden 字段填充。

JSON::make('Address', 'address', [
    Text::make('Street'),
    Text::make('City'),
])->fillAtOnce();

fillOnce() 方法接受一个 Callback,可以在将其添加到模型之前修改数据结构。

// given these fields:
JSON::make('Address', 'address', [
    Text::make('Street'),
    Text::make('City'),
])->fillAtOnce(function ($request, $requestValues, $model, $attribute, $requestAttribute) {
    return ['nested' => $requestValues];
});

// and a request with ['address->street' => 'test str. 5', 'address->city' => 'test city']

// we will get
$requestValues = ['street' => 'test str. 5', 'city' => 'test city'];

// which will be pased into the fillAtOnce callback leading to the following in our db:
['address' => ['nested' => ['street' => 'test str. 5', 'city' => 'test city']]];

可空字段

与其他字段一样,您可以直接在 JSON 字段上调用 nullable()nullValues() 来使包含的所有字段可空,并指定哪些值被视为 null

JSON::make('Address', 'address', [
    Text::make('Street'),
    Text::make('City'),
])->nullable()->nullValues(['_', 0])

标签和属性

默认情况下,您提供给 JSON 字段的第一个参数将被视为其 name。如果您没有提供第二个字符串参数,字段的 attribute 将被猜测,例如 'Some Json Column Name' => 'some_json_column_name'。如果您想使字段的 nameattribute 不同,您可以为字段提供第二个参数,并提供字段作为第三个参数:JSON::make('Some Name', 'column_name', [])

嵌套结构

JSON 字段也可以嵌套自身来显示嵌套的 JSON 结构。

JSON::make('Meta', [
    Text::make('Street'),

    JSON::make('Location', [
        Text::make('Latitude'),
        Text::make('Longitude'),
    ]),
]);

在面板中使用

为了在 Nova 面板中使用 JSON 列,您需要获取顶层 JSON 字段的 'data' 属性。

示例

  1. 面板中只有 JSON 字段
new Panel('Brand Settings', 
    JSON::make('brand_settings', [
        Image::make('Logo')->disk('public'),
        Color::make('Primary Color')->swatches(),
        Color::make('Secondary Color')->swatches(),
    ])->data,
),
  1. 如果您需要在面板中添加其他字段,可以使用点号运算符,如下所示
new Panel('Brand Settings', [
    Text::make('Some Field'),
    ...JSON::make('brand_settings', [
        Image::make('Logo')->disk('public'),
        Color::make('Primary Color')->swatches(),
        Color::make('Secondary Color')->swatches(),
    ])->data,
]),

测试

使用以下命令运行测试

vendor/bin/phpunit

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

致谢

安全

如果您发现任何与安全相关的问题,请通过电子邮件发送至 krishan.koenig@googlemail.com,而不是使用问题跟踪器。

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 获取更多信息。