是否有任何 "options" 来过滤 "checklist" 字段的查询?喜欢 select2_multiple。但我想使用清单而不是 select2_multiple

Is there any "options" to filter query for "checklist" field? Like on select2_multiple. But I want to use checklist instead of select2_multiple

我使用 select2_multiple,我可以像文档示例中那样使用 "options",但我可以在清单字段中使用 "options" 吗?

我尝试添加 "options" 但没有任何效果

$this->crud->addField([
            'label'     => 'Printers',
            'type'      => 'checklist',
            'name'      => 'printer',
            'entity'    => 'printers',
            'attribute' => 'printer_name',
            'model'     => "App\Models\Printer",
            'pivot'     => true,
            'options'   => (function ($query) {
                return $query->where('id', backpack_auth()->user()->id)->get();
            }),
        ]);

我要过滤结果

checklist 字段不允许开箱即用,但您可以创建一个自定义字段,该字段将通过从 select2_multiple.blade.php 中获取相关代码并将其与 checklist.blade.php

resources/views/vendor/backpack/crud/fields/checklist_filtered.blade.php 创建一个包含以下内容的文件:

<!-- checklist with optional filtered query -->
<!-- checklist_filtered -->
@php
    if (!isset($field['options'])) {
        $options = $field['model']::all();
    } else {
        $options = call_user_func($field['options'], $field['model']::query());
    }
@endphp

<div @include('crud::inc.field_wrapper_attributes') >
    <label>{!! $field['label'] !!}</label>
    @include('crud::inc.field_translatable_icon')
    <?php $entity_model = $crud->getModel(); ?>

    <div class="row">
        @foreach ($options as $connected_entity_entry)
            <div class="col-sm-4">
                <div class="checkbox">
                  <label>
                    <input type="checkbox"
                      name="{{ $field['name'] }}[]"
                      value="{{ $connected_entity_entry->getKey() }}"

                      @if( ( old( $field["name"] ) && in_array($connected_entity_entry->getKey(), old( $field["name"])) ) || (isset($field['value']) && in_array($connected_entity_entry->getKey(), $field['value']->pluck($connected_entity_entry->getKeyName(), $connected_entity_entry->getKeyName())->toArray())))
                             checked = "checked"
                      @endif > {!! $connected_entity_entry->{$field['attribute']} !!}
                  </label>
                </div>
            </div>
        @endforeach
    </div>

    {{-- HINT --}}
    @if (isset($field['hint']))
        <p class="help-block">{!! $field['hint'] !!}</p>
    @endif
</div>

然后更新您对 addField 的调用以使用 'type' => 'checklist_filtered',