ericphamhoang/belongs-to-many-field

字段中的 belongsToMany nova 表示。

0.6.1 2019-09-25 02:39 UTC

This package is auto-updated.

Last update: 2024-09-25 13:53:44 UTC


README

将 Belongs To Many 字段用于表示字段中的多对多关系。此字段允许轻松附加关系,您可以将查询传递到多重选择中。

image

安装

composer require benjacho/belongs-to-many-field

弃用

方法 relationModel() 不再需要,为防止冲突它将保留。并且 HasBelongsToMany 特性也不再需要,两者都将存放在仓库中,但不会工作。

用法

在 nova 1.0 中使用 0.3,在 nova 2.0 中使用 0.4 及以上版本。

在资源中您需要传递

  • 方法 make (标签,多对多关系,Nova 资源关系)
  • 方法 options (在这里您需要传递要渲染在多重选择中的选项,您可以通过传递查询来实现,使用 get() 方法来完成此目的)
  • 您不需要传递 onlyOnForms(),这是默认的。
use Benjacho\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
    BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->options(\App\Role::all()),
}

可选

  • 方法 optionsLabel('columnName'),当您没有 'name' 列在您的表中,并且希望用另一个列名作为标签时使用此方法。默认情况下,它通过 'name' 标签跟踪。
use Benjacho\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
    BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->options(\App\Role::all())->optionsLabel('title'),
}
  • 方法 isAction(),当您需要在操作中使用此字段时使用此方法,这会将字段高度设置为 350px,并将其转换为操作。
use Benjacho\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
    BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->options(\App\Role::all())->isAction(),
}

要获取在操作中发送的数据,请这样做

public function handle(ActionFields $fields, Collection $models)
{
    // Get the expenseTypes from the request because the Field BelongsToManyField does not send it
    
    $values = array_column(json_decode(request()->roles, true),'id');
    
    foreach ($models as $model) {
        $model->roles()->sync($values);
    }
}

验证

此包实现了所有 Laravel 验证,您需要在 rules 方法中传递规则,规则列在 Laravel 验证规则数组中*。

use Benjacho\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
    BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->options(\App\Role::all())->relationModel(\App\User::class)->rules('required', 'min:1', 'max:5', 'size:3' new CustomRule),
}

image

对于这些验证的翻译,请使用正常的 Laravel 验证翻译。

待办事项

实现验证,实现自定义规则

贡献

- 提交拉取请求 - 问题 - 或联系我: christianbfc97@gmail.com