naoray/nova-json

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

支持包维护!
naoray

安装数: 311,542

依赖: 1

建议者: 0

安全: 0

星标: 35

关注者: 5

分支: 20

开放问题: 1

类型:package

v3.1.0 2024-07-20 11:40 UTC

This package is auto-updated.

Last update: 2024-09-20 11:57:31 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. 如果您需要在面板中添加其他字段,可以使用 splat 运算符,如下所示
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)。有关更多信息,请参阅 许可证文件