zetrider/nova-inputs-field

1.0.1 2021-10-28 18:06 UTC

This package is auto-updated.

Last update: 2024-09-29 01:02:27 UTC


README

基于json的多个字段(Laravel Nova)

  • 基于json创建多个字段
  • 可用的自定义属性
  • 工具库:输入、选择、复选框、单选按钮

需求

  • php: >=7.1
  • laravel/nova: ^3.0

截图

Code example 1

Form example 1

Code example 2

Form example 2

安装

通过Composer安装

composer require zetrider/nova-inputs-field

用法

数据库

数据库中的字段应基于文本类型。

例如

$table->text('fields')->nullable();
// Or
$table->longText('fields')->nullable();
// Or
$table->json('fields')->nullable();

字段以json格式存储数据。

例如

[{"exampleTest":"Hello","exampleColor":"#005a9e","exampleDate":"2021-10-28","exampleSelect":"value1","exampleMultiple":["value2","value3"],"exampleCheckbox":["value1","value2"],"exampleRadio":"value3"}]

数据

  • 键:(string) 字段键
  • 值:(array | string) - 取决于字段类型和 'multiple' 属性。复选框始终为数组。

模型

您的模型应转换字段

/**
    * The attributes that should be cast.
    *
    * @var array
*/
protected $casts = [
    'fields' => 'array',
];

Nova 资源

NovaInputsField 的工作方式与标准 Nova 字段类似。

您可以使用以下方法

  • input(string $key, array $attributes)
  • select(string $key, array $attributes, array $options)
  • checkbox(string $key, array $attributes, array $options)
  • radio(string $key, array $attributes, array $options)

$key - 在json中使用作为键

$attributes - 字段支持的任何html属性。复选框和单选按钮字段的占位符属性用作标题。

$options - 字段的选项。数组键是选项的值。数组值是选项名称。

例如

use ZetRider\NovaInputsField\NovaInputsField;

public function fields(Request $request)
{
    return [
        NovaInputsField::make('Some field', 'fields')
            // Simple text field
            ->input('exampleTest', ['type' => 'text', 'placeholder' => 'Type here...'])
            // Color field
            ->input('exampleColor', ['type' => 'color'])
            // Date field
            ->input('exampleDate', ['type' => 'date', 'min' => now()->format('Y-m-d')])
            // Simple select
            ->select('exampleSelect', ['placeholder' => 'Select option...'], [
                'value1' => 'Option1',
                'value2' => 'Option2',
                'value3' => 'Option3'
            ])
            // Multiple select
            ->select('exampleMultiple', ['placeholder' => 'Select option...', 'multiple' => 'multiple', 'style' => 'height: 100px;'], [
                'value1' => 'Option1',
                'value2' => 'Option2',
                'value3' => 'Option3'
            ])
            // Checkbox
            ->checkbox('exampleCheckbox', ['placeholder' => 'Choice option'], [
                'value1' => 'Check1',
                'value2' => 'Check2',
                'value3' => 'Check3'
            ])
            // Radio
            ->radio('exampleRadio', ['placeholder' => 'Some title'], [
                'value1' => 'Radio1',
                'value2' => 'Radio2',
                'value3' => 'Radio3'
            ]),
    ];
}

NovaInputsField::make('Prices', 'prices')
    ->select('type', [], ['base' => 'Base', 'sale' => 'Sale'])
    ->input('price', ['type' => 'nubmer', 'min' => '1', 'step' => '0.01']),

许可证

本项目是开源软件,根据 MIT 许可证 发布。