moonshine/layouts-field

用于MoonShine的重复字段组

1.0.2 2024-08-23 07:17 UTC

This package is auto-updated.

Last update: 2024-09-23 07:25:09 UTC


README

MoonShine的布局字段

快速入门

安装

composer require moonshine/layouts-field

用法

MoonShine的字段布局允许您轻松管理重复的字段组。您将能够添加、删除和排序由基本字段组成的组。在布局字段中使用字段有一些限制。您可以使用任何基本字段,除了关系字段。

use MoonShine\Layouts\Fields\Layouts;

Layouts::make('Content')
    ->addLayout('Contact information', 'contacts', [
        Text::make('Name'),
        Email::make('Email'),
    ])
     ->addLayout('Banner section', 'banner', [
        Text::make('Title'),
        Image::make('Banner image', 'thumbnail'),
    ]),

添加布局

您可以使用以下方法在您的布局字段中添加布局:

addLayout(string $title, string $name, iterable $fields, ?int $limit = null)
  1. $title参数允许您指定将在表单中显示的字段组名称。
  2. $name参数用于在字段值中存储所选布局。
  3. $fields参数接受一个字段数组,该数组将用于填充表单中的字段组。
  4. $limit允许您设置字段中组数的最大值。

添加类型转换

字段将其值存储为单个JSON字符串。要使用布局字段,您需要为您的模型添加类型转换。

use MoonShine\Layouts\Casts\LayoutsCast;

class Article extends Model
{
    protected $casts = [
        'content' => LayoutsCast::class,
    ];
}

Layouts::make('Content', 'content')
    ->addButton(ActionButton::make('New layout')->icon('heroicons.outline.plus')->primary())

自定义按钮标签

您可以使用ActionButton组件更改默认的“添加布局”按钮文本。

Layouts::make('Content')
    ->addButton(ActionButton::make('New layout')->icon('heroicons.outline.plus')->primary())

添加搜索字段

您可以在布局列表中添加搜索输入如下:

Layouts::make('Content')
  ->addLayout('Info section', 'info', [
    ...
  ])
  ...
  ->addLayout('Slider section', 'slider', [
    ...
  ])
  ->searchable()