outl1ne / nova-dependency-container
一个 Laravel Nova 字段容器,允许依赖于其他字段的值
Requires
- php: >=8.0
- laravel/nova: ^4.0
README
此 Laravel Nova 包添加了一个容器,用于分组依赖于其他字段值的字段。
要求
php: >=8.0
laravel/nova: ^4.0
屏幕截图
安装
使用 Composer 在 Laravel Nova 项目中安装此包
composer require outl1ne/nova-dependency-container
用法
- 将
Outl1ne\DependencyContainer\HasDependencies
特性添加到您的 Nova 资源中。 - 将
Outl1ne\DependencyContainer\DependencyContainer
字段添加到您的 Nova 资源中。 - 将
Outl1ne\DependencyContainer\ActionHasDependencies
特性添加到您希望使用依赖的 Nova 动作中。
class Page extends Resource { use HasDependencies; public function fields(Request $request) { return [ Select::make('Name format', 'name_format')->options([ 0 => 'First Name', 1 => 'First Name / Last Name', 2 => 'Full Name' ])->displayUsingLabels(), DependencyContainer::make([ Text::make('First Name', 'first_name') ])->dependsOn('name_format', 0), ]; } }
依赖
此包支持四种类型的依赖
->dependsOn('field', 'value')
->dependsOnNot('field', 'value')
->dependsOnEmpty('field')
->dependsOnNotEmpty('field')
->dependsOnNullOrZero('field')
可以通过在 DependencyContainer
上链式调用方法来组合这些依赖
DependencyContainer::make([ // dependency fields ]) ->dependsOn('field1', 'value1') ->dependsOnNotEmpty('field2') ->dependsOn('field3', 'value3')
用作依赖的字段可以是任何 Laravel Nova 字段类型。目前只支持两种关系字段类型,即 BelongsTo
和 MorphTo
。
以下是一个使用复选框的示例
BelongsTo 依赖
如果我们参考 Novas 文档中的一个例子,即 Post 模型属于 User 模型,可以从 Novas 文档 BelongsTo 部分找到,依赖设置具有以下结构。
我们使用 belongsTo
资源的单数形式,以小写形式表示,在这个例子中,Post
变为 post
。然后我们用点表示法定义我们想要依赖的资源属性。在这个例子中,我们只使用 id
属性,即 post.id
。
BelongsTo::make('Post'), DependencyContainer::make([ Boolean::make('Visible') ]) ->dependsOn('post.id', 2)
当选择具有 id
为 2 的 Post
资源时,将出现一个 Boolean
字段。
BelongsToMany 依赖
BelongsToMany 设置类似于 BelongsTo。
dependsOn
方法应指向中间表的名称。如果它被命名为 role_user
,设置应该是
BelongsToMany::make('Roles') ->fields(function() { return [ DependencyContainer::make([ // pivot field rules_all Boolean::make('Rules All', 'rules_all') ]) ->dependsOn('role_user', 1) ] }),
如果枢轴字段名称出现多次,请考虑使用 自定义中间表模型 并在适当的模型关系方法中定义它。我发现唯一可靠的解决方案是使用突变来获取/设置被多次使用的字段。虽然这看起来很丑,但使用观察者时,应该在中间模型实例上触发的事件可能在每个 Nova 新版本发布时不可靠。
如果 Nova 在中间表上可靠地触发 Eloquent 事件,我将更新此示例,使用事件而不是突变来提供一个更优雅的解决方案。
以下是一个使用枢轴字段名为 type
的中间表进行 get/set 突变的(丑陋)示例。
// model User class User ... { public function roles() { return $this->belongsToMany->using(RoleUser::class)->withPivot('rules_all'); } } // model Role class Role ... { public function users() { return $this->belongsToMany->using(RoleUser::class)->withPivot('rules_all'); } } // intermediate table use Illuminate\Database\Eloquent\Relations\Pivot; class RoleUser extends Pivot { protected $table 'role_user'; public function getType1Attribute() { return $this->type; } public function setType1Attribute($value) { $this->attributes['type'] = $value; } // ... repeat for as many types as needed }
现在轮到依赖容器了。
->fields(function() { return [ DependencyContainer::make([ // pivot field rules_all Select::make('Type', 'type_1') ->options([ /* some options */ ]) ->displayUsingLabels() ]) ->dependsOn('role_user', 1) , DependencyContainer::make([ // pivot field rules_all Select::make('Type', 'type_2') ->options([ /* different options */ ]) ->displayUsingLabels() ]) ->dependsOn('role_user', 2) , // .. and so on ] }),
MorphTo 依赖
从 Novas 文档中获取的类似示例称为 commentable。它使用 3 个模型:Comment
、Video
和 Post
。在这里,Comment
有可变字段 commentable_id
和 commentable_type
。
对于 MorphTo
依赖,需要以下结构。
Commentable
变为小写 commentable
,依赖的值取决于资源的单数形式。在此示例中,只有当选择 Post
资源时,依赖容器才会添加两个额外的字段,即 Additional Text
和 Visible
。
MorphTo::make('Commentable')->types([ Post::class, Video::class, ]), DependencyContainer::make([ Text::make('Additional Text', 'additional'), Boolean::make('Visible', 'visible') ]) ->dependsOn('commentable', 'Post')
许可证
本项目是开源软件,遵循 MIT 许可协议。