handleglobal/nova-nested-form

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

v3.0.12 2021-05-26 14:40 UTC

README

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

Nova Nested Form

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

安装

使用git引用将包添加到您的composer中

"repositories": [
        {
            "url": "https://github.com/handleglobal/laravel-nova-nested-form.git",
            "type": "git"
        }
]
composer require handleglobal/nova-nested-form:dev-master

贡献

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

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

简单地将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 Handleglobal\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 ],

                    // Integration for nova booleanGroup field
                    [ 'attribute' => 'my_multiple_checkbox', 'booleanGroup' => 'the_checkbox_key_to_target' ],
                ];
            })
        ];
    }
}

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

  • 不是
  • 为空
  • 不为空
  • 大于
  • 大于等于
  • 小于
  • 小于等于
  • 包含
  • 布尔组

添加最小或最大子数

例如,如果您希望每个用户至少有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('\'),