定制 naoray/nova-json 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

naoray/nova-json

Composer 安装命令:

composer require naoray/nova-json

包简介

Nova json field to spread a json column throughout multiple fields.

关键字:

README 文档

README

Software License Total Downloads Tests

The JSON field wrapper allows you to specify multiple fields which will be resolved into a single model attribute. This allows you to validate every information you store inside a json column seperately.

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

The above will be resolved into a single author attribute on the model.

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

Install & setup

composer require naoray/nova-json

Add the column's name, you want to use in the JSON field, to your $casts array on the resource's model!

Usage

You can destructure one JSON column into multiple Nova fields and apply unique rules to each of the key-value pairs.

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 callbacks

The ->fillUsing() callbacks are normally used to fill the models attribute directly. With this package, it's not necessary to fill the model's attribute, but instead you should return the value you want to save on the model itself.

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

The above example is rather silly than useful, but it demonstrates the concept. The _ Foo_ value will be apended to every address->street value within nova.

Fill at once

When using data transfer objects (which works well with castable dto's) you don't want each field to be filled seperately, because than the dto's validation is useless. With the fillAtOnce() method a Hidden field will be added and the filling of single fields will be avoided. Instead all values will be filled at once via the Hidden field.

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

The fillOnce() method accepts a Callback which can be used to modify the data structure before it is added to the model.

// 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']]];

Nullable Fields

As with other fields you can call nullable() and nullValues() on the JSON field directly to make all fields contained nullable and specify which values are treated as null

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

Labels and Attributes

By default the first argument you provide the JSON field will be considered its name. If you don't provide a second string argument the attribute of the field will be guessed e.g. 'Some Json Column Name' => 'some_json_column_name'. If you want your field name to be different from your attribute you can provide the field with a second argument and provide the fields as the third argument: JSON::make('Some Name', 'column_name', [])

Nested Structures

The JSON field can also be nested by itself to display nested JSON structures:

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

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

Use inside Panels

In order to use JSON column inside Nova Panel you need to get 'data' property of the top level JSON field.

Examples

  1. JSON is the only field inside Panel
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. if you need other fields inside the Panel you can use splat operator like this:
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,
]),

Testing

Run the tests with:

vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

Security

If you discover any security-related issues, please email krishan.koenig@googlemail.com instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

naoray/nova-json 适用场景与选型建议

naoray/nova-json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 504.29k 次下载、GitHub Stars 达 37, 最近一次更新时间为 2020 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「nova」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 naoray/nova-json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 naoray/nova-json 我们能提供哪些服务?
定制开发 / 二次开发

基于 naoray/nova-json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 504.29k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 37
  • 点击次数: 25
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 37
  • Watchers: 5
  • Forks: 28
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-18