我使用select2_multiple,并且可以像文档示例中那样使用“选项”,但是我可以在清单字段中使用“选项”吗?
我尝试添加“选项”,但没有任何效果
$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();
}),
]);
我想过滤结果
答案 0 :(得分:1)
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',