keops007/belongs-to-many-field

字段中属于多个nova表示。


README

灵感来源于:https://github.com/Benjacho/belongs-to-many-field-nova 初始包由于 "sortableUriKey" Belongs To Many 字段在字段中表示多对多关系而无法与较老版本的Nova兼容。此字段允许轻松关联关系。您还可以

  • 将查询传递到多选框
  • 依赖于BelongsTo字段
  • 它可在索引、详情和表单中使用!

安装

composer require keops007/belongs-to-many-field

使用方法

在您需要的资源中传递

  • 方法make ('标签', '多对多关系函数名称', 'Nova资源关系')
use Keops007\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 Keops007\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'),

验证

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

use Keops007\BelongsToManyField\BelongsToManyField;

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

对于此验证的翻译,使用正常的Laravel验证翻译。

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

感谢:https://github.com/Benjacho/belongs-to-many-field-nova