glacom / nova-json
Nova json 字段可以将 json 列扩展到多个字段。
Requires
- php: >=8.0
- laravel/nova: ^4.0
README
此包旨在用于 Laravel Nova 4 版本,原始存储库在创建时未支持 Nova 4,作者不接受 Nova 4 的 PR 原始存储库 https://github.com/Naoray/nova-json
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 stepanenko3/nova-json
将要在 JSON 字段中使用的列名添加到资源模型上的 $casts 数组中!
用法
您可以将一个 JSON 列解构为多个 Nova 字段,并对每个键值对应用唯一的规则。
use Stepanenko3\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'), ]); ] }
填充Using 回调
->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'。如果您希望字段的 name 与 attribute 不同,可以提供第二个参数并将字段作为第三个参数提供: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' 属性。
示例
- JSON 是面板中唯一的字段
new Panel('Brand Settings', JSON::make('brand_settings', [ Image::make('Logo')->disk('public'), Color::make('Primary Color')->swatches(), Color::make('Secondary Color')->swatches(), ]), ),
- 如果您需要在面板中使用其他字段,您可以使用 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(), ]), ]),
更新日志
请参阅变更日志以获取更多关于最近更改的信息。
贡献
请参阅贡献指南以获取详细信息。
致谢
安全
如果您发现任何安全相关的问题,请使用问题跟踪器或拉取请求。
许可
MIT 许可证(MIT)。请参阅许可文件以获取更多信息。