optimistdigital/nova-dependency-container

一个 Laravel Nova 字段容器,允许依赖其他字段的值

2.0.4 2023-07-24 08:43 UTC

This package is auto-updated.

Last update: 2024-09-24 10:55:32 UTC


README

Latest Version on Packagist Total Downloads License

这个 Laravel Nova 包添加了一个用于分组依赖其他字段值的字段的容器。

要求

  • php: >=8.0
  • laravel/nova: ^4.0

截图

Screenshots

安装

通过 Composer 在 Laravel Nova 项目中安装此包

composer require outl1ne/nova-dependency-container

用法

  1. Outl1ne\DependencyContainer\HasDependencies 特性添加到您的 Nova 资源中。
  2. Outl1ne\DependencyContainer\DependencyContainer 字段添加到您的 Nova 资源中。
  3. 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),

        ];
    }
}

依赖关系

该包支持四种类型的依赖

  1. ->dependsOn('field', 'value')
  2. ->dependsOnNot('field', 'value')
  3. ->dependsOnEmpty('field')
  4. ->dependsOnNotEmpty('field')
  5. ->dependsOnNullOrZero('field')

这些依赖可以通过在 DependencyContainer 上链式调用方法进行组合

DependencyContainer::make([
  // dependency fields
])
->dependsOn('field1', 'value1')
->dependsOnNotEmpty('field2')
->dependsOn('field3', 'value3')

用作依赖的字段可以是任何 Laravel Nova 字段类型。目前仅支持两种关系字段类型,即 BelongsToMorphTo

以下是一个使用复选框的示例

Demo

BelongsTo 依赖关系

如果我们遵循 Novas 文档中 BelongsTo 的示例,即一个 Post 模型属于一个 User 模型,以下构造如下。

我们使用单数形式的 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 的枢轴字段的自定义生成器设置示例。

// 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 个模型:CommentVideoPost。在这里 Comment 有可变字段 commentable_idcommentable_type

对于 MorphTo 依赖,需要以下结构。

Commentable 转换为小写 commentable,依赖的值是资源单数形式。在这个例子中,只有当选择 Post 资源时,依赖容器才会添加两个额外的字段,即 Additional TextVisible

MorphTo::make('Commentable')->types([
    Post::class,
    Video::class,
]),

DependencyContainer::make([
    Text::make('Additional Text', 'additional'),
    Boolean::make('Visible', 'visible')
])
->dependsOn('commentable', 'Post')

许可证

此项目是开源软件,许可协议为 MIT 许可证