定制 rsubr/nova-json-schema-field 二次开发

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

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

rsubr/nova-json-schema-field

Composer 安装命令:

composer require rsubr/nova-json-schema-field

包简介

A Laravel Nova field for JSON Schema.

README 文档

README

Packagist

Laravel Nova field for displaying JSON schema data. Inspired by nsavinov/nova-json-schema-field.

Installation

You can install the package into a Laravel app that uses Nova via composer:

composer require rsubr/nova-json-schema-field

Usage

In the Laravel Model:

    use Illuminate\Database\Eloquent\Casts\AsCollection;

    protected $casts = [
        'details' => AsCollection::class,
    ];

    // NovaJsonSchemaField does not like empty JSON, so use an empty placeholder
    protected $attributes = [
        'details' => '{"_": ""}',
    ];

Using a Static Schema

In the Nova resource:

use Rsubr\NovaJsonSchemaField\NovaJsonSchemaField;

public function fields(Request $request)
{
    return [
        // ...
        NovaJsonSchemaField::make('Details')
            ->jsonSchema($this->loadSchema())
            ->rules('json'),
    ];
}

private function loadSchema(): array
{
    $schema = <<<SCHEMA
        {
            "type": "object",
            "required": [
                "event_name",
                "level",
                "start_date",
                "duration"
            ],
            "properties": {
                "event_name": {
                    "description": "Event Name"
                },
                "level": {
                    "type": "array",
                    "items": {
                        "enum": [
                            "State Level",
                            "National",
                            "International"
                        ]
                    },
                    "description": "Event level"
                },
                "start_date": {
                    "type": "string",
                    "format": "date",
                    "description": "Start Date"
                },
                "duration": {
                    "type": "number",
                    "description": "Duration in Days"
                }
            }
        }

    SCHEMA;
    return json_encode($schema, true);
}

Using a Dynamic Schema

The schema can be dynamically loaded from a related Model or API.

Eg. to load the JSON schema from an EventType Model, first create an attribute on the Model and call from the Nova resource.

In the Laravel Model:

    public function getJsonSchemaAttribute() {
        return $this->event_type->json_schema;
    }

The event_type Model should have an HasMany relationship with the model holding the JSON data.

In the Nova Resource:

    protected function loadSchema($request)
    {
        // NovaJsonSchemaField does not like empty JSON, so use an empty placeholder
        $schema = $request->findModelQuery()->first()->json_schema ?? array();

        return $schema;
    }

Notes

  1. For Postgresql, use store the JSON Schema in a JSON column to preserve field order, do not use JSONB for the schema. The JSON data values can be stored in JSON or JSONB.

统计信息

  • 总下载量: 235
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: Vue

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固