hmayer/json-field

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

安装: 2 009

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 1

类型:package

v1.0.0 2023-05-31 18:44 UTC

This package is auto-updated.

Last update: 2024-09-06 15:46:53 UTC


README

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 hmayer/json-field

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

用法

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

use Hmayer\JsonField\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()回调通常用于直接填充模型的属性。使用此包时,不需要填充模型的属性,而应返回要保存到模型本身上的值。

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

上述示例相当愚蠢,但演示了概念。_ Foo_ 值将被附加到nova中的每个address->street值。

一次性填充

当使用数据传输对象(与可铸数据传输对象配合使用效果良好)时,您不希望每个字段单独填充,因为那样的话,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(),
    ]),
),
  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(),
    ]),
]),

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

贡献

请参阅贡献指南以获取详细信息。

鸣谢

安全

如果您发现任何与安全相关的漏洞,请使用问题跟踪器或拉取请求。

许可证

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