我可以设置CRUD Controller来显示字段,具体取决于正在编辑的模型吗?
例如:我有一个域模型:id
,type
,field1
,field2
。
对于type=type1
仅显示的模型field1
:
$this->crud->addFields([
['name' => 'field1', 'label' => 'field1 label']
]);
type=type2
仅适用于的型号field2
:
$this->crud->addFields([
['name' => 'field2', 'label' => 'field2 label']
]);
对于type=type3
同时具有field1
和的模型field2
:
$this->crud->addFields([
['name' => 'field1', 'label' => 'field1 label'],
['name' => 'field2', 'label' => 'field2 label']
]);
在自定义字段类型中,可以使用以下变量:
...
$ entry-在Update操作中,当前条目被修改(实际值);
实现此目的的一种方法是使用自定义字段并利用该$entry
变量。例如,您可以创建2个自定义字段,如下所示:
field1.blade.php
@if(in_array($entry->type, ['type1','type3']))
{{-- your field content here, see resources/views/vendor/backpack/crud/fields/text.blade.php as an example --}}
@endif
field2.blade.php
@if(in_array($entry->type, ['type2','type3']))
{{-- you can even pull in the content of an existing field like this --}}
@include('crud::fields.text')
@endif
然后,在您的控制器中,您总是要同时添加两个字段,并让这些字段自己进行隐藏适当字段的工作。
$this->crud->addFields([
[
'name' => 'field1',
'label' => 'field1 label',
'type' => 'field1',
],
[
'name' => 'field2',
'label' => 'field2 label',
'type' => 'field2',
]
]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句