makogai / nova-dependency-container
一个 Laravel Nova 字段容器,允许依赖于其他字段的值
Requires
- php: >=7.1.0
- dev-master
- 1.3.68
- 1.3.67
- 1.3.66
- 1.3.65
- 1.3.64
- 1.3.63
- 1.3.62
- 1.3.61
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4.1
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.12
- 1.2.11
- 1.2.10
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-fix-actions
- dev-development
This package is auto-updated.
Last update: 2024-09-10 17:56:11 UTC
README
描述
一个用于分组依赖其他字段值的字段容器。可以在任何字段类型或值上设置依赖关系。
演示
版本
- 为 Laravel v5.8 或 v6.x 和 Nova 2.x 安装 v1.2.x
- 为 Laravel v5.7 和 Nova v1.x 安装 v1.1.2
安装
该包可以通过 Composer 安装。
composer require epartment/nova-dependency-container
使用
- 将
Epartment\NovaDependencyContainer\HasDependencies
特性添加到您的 Nova 资源中。 - 将
Epartment\NovaDependencyContainer\NovaDependencyContainer
添加到您的 Nova 资源fields
方法中。 - 将
Epartment\NovaDependencyContainer\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(), NovaDependencyContainer::make([ Text::make('First Name', 'first_name') ])->dependsOn('name_format', 0), ]; } }
依赖关系
该包支持四种类型的依赖关系
->dependsOn('field', 'value')
->dependsOnNot('field', 'value')
->dependsOnEmpty('field')
->dependsOnNotEmpty('field')
->dependsOnNullOrZero('field')
这些依赖关系可以通过在 NovaDependencyContainer
上链式调用方法来组合
NovaDependencyContainer::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'), NovaDependencyContainer::make([ Boolean::make('Visible') ]) ->dependsOn('post.id', 2)
当选择 Post
资源时,一个 Boolean
字段将出现。
BelongsToMany 依赖关系
BelongsToMany 的设置类似于 BelongsTo。
dependsOn
方法应指向中间表的名字。如果它被命名为 role_user
,则设置应为
BelongsToMany::make('Roles') ->fields(function() { return [ NovaDependencyContainer::make([ // pivot field rules_all Boolean::make('Rules All', 'rules_all') ]) ->dependsOn('role_user', 1) ] }),
如果枢轴字段名称出现多次,请考虑使用 自定义中间表模型 并在适当的模型关系方法中定义它。我找到的唯一可靠的解决方案是使用突变器来获取/设置被多次使用的字段。虽然这可能看起来很丑,但在使用观察者时,应该在中间模型实例上触发的每个 Nova 版本的事件都可能不可靠。
如果 Nova 能够可靠地在中间表上触发 Eloquent 事件,我将更新此示例,使用事件而不是突变器提供更优雅的方法。
以下是使用名为 type
的枢轴字段的一个(丑陋的)示例,用于设置中间表的获取/设置突变器。
// 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 [ NovaDependencyContainer::make([ // pivot field rules_all Select::make('Type', 'type_1') ->options([ /* some options */ ]) ->displayUsingLabels() ]) ->dependsOn('role_user', 1) , NovaDependencyContainer::make([ // pivot field rules_all Select::make('Type', 'type_2') ->options([ /* different options */ ]) ->displayUsingLabels() ]) ->dependsOn('role_user', 2) , // .. and so on ] }),
MorphTo 依赖关系
一个类似示例来自 Novas 文档中的 MorphTo,被称为 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, ]), NovaDependencyContainer::make([ Text::make('Additional Text', 'additional'), Boolean::make('Visible', 'visible') ]) ->dependsOn('commentable', 'Post')
许可证
MIT 许可证 (MIT)。更多信息请参阅 许可证文件。