thienkimlove/nova-dependency-container

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

1.2.10 2019-12-01 02:45 UTC

This package is auto-updated.

Last update: 2024-08-29 05:30:55 UTC


README

Latest Version on Packagist Total Downloads License


描述

一个用于分组依赖于其他字段值的字段的容器。可以在任何字段类型或值上设置依赖。


演示

Demo


版本

  • 为 Laravel v5.8 或 v6.x 和 Nova 2.x 安装 v1.2.x
  • 为 Laravel v5.7 和 Nova v1.x 安装 v1.1.2

安装

该软件包可以通过 Composer 安装。

composer require thienkimlove/nova-dependency-container

用法

  1. Thienkimlove\NovaDependencyContainer\HasDependencies 特性添加到您的 Nova 资源中。
  2. Thienkimlove\NovaDependencyContainer\NovaDependencyContainer 添加到您的 Nova 资源 fields 方法中。
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),

        ];
    }
}

依赖项

该软件包支持四种类型的依赖项

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

这些依赖项可以通过在 NovaDependencyContainer 上链式调用方法来组合

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

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

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

Demo


BelongsTo 依赖

如果我们遵循来自 Novas 文档的示例,一个 Post 模型属于 User 模型,在 Novas 文档 BelongsTo 中,依赖项设置的构造如下。

我们使用 belongsTo 资源的单数形式,以小写形式表示,在本例中 Post 变为 post。然后我们使用点表示法,定义我们想要依赖的资源属性。在本例中我们只使用 id 属性,即 post.id

BelongsTo::make('Post'),

NovaDependencyContainer::make([
    Boolean::make('Visible')
])
->dependsOn('post.id', 2)

当选择具有 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 的中间表进行 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 [
		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 个模型:CommentVideoPost。在这里 Comment 有可变字段 commentable_idcommentable_type

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

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

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

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

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件