chiwex/nova-nested-form

一个Laravel Nova包,允许您从父表单创建/更新/删除嵌套相关字段。

v3.0.11 2021-02-22 10:07 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads Daily Downloads

Nova Nested Form

此包允许您将嵌套关系表单包含到父表单中。

安装

composer require yassi/nova-nested-form

贡献

由于我没有预料到这么多人会使用这个包(这很棒),而且我也没有足够的时间经常自己更新/增强这个包,因此我在寻找其他贡献者来帮助我进行维护和功能请求。如果您感兴趣,请随时联系我!

更新到3.0

不再提供 afterFillbeforeFill 方法。

将新的关系表单附加到资源

只需在字段中添加一个NestedForm。第一个参数必须是现有的NovaResource类,第二个参数(可选)必须是模型中的现有HasOneOrMany关系。

namespace App\Nova;

use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Gravatar;
use Laravel\Nova\Fields\Password;
// Add use statement here.
use Yassi\NestedForm\NestedForm;

class User extends Resource
{
    ...
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            Gravatar::make(),

            Text::make('Name')
                ->sortable()
                ->rules('required', 'max:255'),

            Text::make('Email')
                ->sortable()
                ->rules('required', 'email', 'max:254')
                ->creationRules('unique:users,email')
                ->updateRules('unique:users,email,{{resourceId}}'),

            Password::make('Password')
                ->onlyOnForms()
                ->creationRules('required', 'string', 'min:6')
                ->updateRules('nullable', 'string', 'min:6'),

            // Add NestedForm here.
            NestedForm::make('Posts'),
        ];
    }

选择何时显示表单

例如,如果嵌套表单只有在"has_comments"属性的值为true时才可用,您可以使用

class Post extends Resource
{
    ...
    public function fields(Request $request)
    {
        return [
            Boolean::make('Has Comments'),
            NestedForm::make('Comments')->displayIf(function ($nestedForm, $request) {
               return [
                    [ 'attribute' => 'has_comments', 'is' => true ]
               ];
        ];
    }
})

displayIf 方法期望返回一个数组数组,因为您可能想要添加多个条件。

class Post extends Resource
{
    ...
    public function fields(Request $request)
    {
        return [
            Boolean::make('Has Comments'),
            Text::make('Title'),
            Text::make('Subtitle')->nullable(),
            Number::make('Number of comments allowed'),
            NestedForm::make('Comments')->displayIf(function ($nestedForm, $request) {
                return [
                    [ 'attribute' => 'has_comments', 'is' => true ],
                    [ 'attribute' => 'title', 'isNotNull' => true ],
                    [ 'attribute' => 'subtitle', 'isNull' => true ],
                    [ 'attribute' => 'title', 'includes' => 'My' ],
                    [ 'attribute' => 'number_of_comments_allowed', 'moreThanOrEqual' => 1 ],
                ];
            })
        ];
    }
}

然后,该包将添加这些条件,并在您填写字段时动态更新您的表单。可用的规则是

  • 不是
  • 为空
  • 不为空
  • 大于
  • 大于等于
  • 小于
  • 小于等于

添加最少或最多的子元素数量

例如,如果您希望每个用户至少有3篇帖子,最多有5篇帖子,只需使用

NestedForm::make('Posts')->min(3)->max(5),

请注意,该包会自动检测关系是否接受多个子元素或单个子元素,并相应地设置最大值。

在创建新用户时,将显示3个空白帖子。如果您达到帖子数量的最大值,"添加新帖子"按钮将消失。

设置默认展开/折叠行为

如果您希望嵌套表单默认展开,只需使用

NestedForm::make('Posts')->open(true),

修改默认标题

您可以使用 heading() 方法修改默认标题。您可以使用辅助方法 wrapIndex() 将当前子索引添加到您的标题中。

NestedForm::make('Posts')->heading(NestedForm::wrapIndex() . ' // Post'),

您还可以使用辅助方法 wrapAttribute() 将当前子元素的任何属性添加到您的标题中。

NestedForm::make('Posts')->heading(NestedForm::wrapIndex() . ' // ' . NestedForm::wrapAttribute('title', 'My default title')),

修改索引分隔符

当您有嵌套表单时(例如,1. 帖子,1.1. 评论,1.1.1. 点赞),您可以在 separator() 方法中修改默认索引分隔符。

NestedForm::make('Posts')->separator('\'),