kraenkvisuell/belongs-to-many-field

字段中属于多对多关系。

v2.0 2021-05-17 10:41 UTC

This package is auto-updated.

Last update: 2024-08-29 05:38:00 UTC


README

属于多字段用于在字段中表示多对多关系。此字段允许轻松关联关系。您还可以

  • 向多选查询传递查询
  • 依赖于BelongsTo字段
  • 它在索引、详细信息和表单中都可使用!

image

安装

composer require kraenkvisuell/belongs-to-many-field

用法

在您需要的资源中传递

  • 方法 make ('标签', '多对多关系函数名', 'Nova资源关系')
use Kraenkvisuell\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
    return [
        ..., //If you are using with BelongsToMany native Field, put this field after

        BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role'),
    ];
}

函数

  • 方法 optionsLabel('列名'), 当您的表中没有 'name' 列,并且您想通过另一个列名进行标签化时,此方法被使用。默认情况下,它通过标签 'name' 进行跟踪。

重要

  • 如果您想在表单显示时通过另一个列名进行标签化,您需要在您的关联资源上设置 title() 方法,此方法返回用于标签化的字符串,同时不要忘记添加 optionsLabel() 方法以在详细信息和索引中显示。
use Kraenkvisuell\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
    BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->optionsLabel('full_role_name'),
}
  • 获取在操作中发送的数据
public function handle(ActionFields $fields, Collection $models)
{
    //note that roles is the many to many relationship function name
    $values = array_column(json_decode(request()->roles, true),'id');
    
    foreach ($models as $model) {
        $model->roles()->sync($values);
    }
}
     BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')
     ->options(\App\Role::all())
     ->setMultiselectProps([
        'selectLabel' => 'click for select',
        // and others from docs
     ]);
  • 方法 dependsOn($dependsOnvalue, $dependsOnKey), 此方法允许您依赖于 belongsto 字段,从而实现自动查询
     BelongsTo::make('Association', 'association', 'App\Nova\Association'),

     BelongsToManyField::make('Participants', 'participant', 'App\Nova\Participant')
     ->dependsOn('association', 'association_id'),
  • 方法 canSelectAll($messageSelectAll), 此方法允许您显示全选复选框,如果不传递消息,则默认显示
     BelongsToManyField::make('Participants', 'participant', 'App\Nova\Participant')
     ->canSelectAll('Seleccionar Todo'),
  • 方法 showAsListInDetail(), 此方法允许您将默认视图更改为详细信息中的列表
     BelongsToManyField::make('Participants', 'participant', 'App\Nova\Participant')
     ->showAsListInDetail(),

验证

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

use Kraenkvisuell\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
    return [
        ...,
        BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->rules('required', 'min:1', 'max:5', 'size:3', new CustomRule()),
    ];
}

image

翻译

发布翻译

php artisan vendor:publish --provider="Kraenkvisuell\BelongsToManyField\FieldServiceProvider" 

此包包含以下 vue-multiselect 插件的翻译。

  • en, ru.

使用 Laravel 验证翻译进行翻译。

致谢: https://github.com/manmohanjit/nova-belongs-to-dependency