背包CRUD控制器:根据编辑的模型显示字段

时间:2019-04-30 19:15:04

标签: laravel backpack-for-laravel

我可以设置CRUD Controller来根据编辑的模型来显示字段吗?

示例:我的模型具有以下字段:idtypefield1field2

对于带有type=type1的模型,我只想显示field1

$this->crud->addFields([
    ['name' => 'field1', 'label' => 'field1 label']
]);

对于仅具有type=type2 field2的模型:

$this->crud->addFields([
    ['name' => 'field2', 'label' => 'field2 label']
]);

对于同时具有type=type3field1的{​​{1}}的模型:

field2

1 个答案:

答案 0 :(得分:1)

this page in the docs的最底部列出:

  

在自定义字段类型中,您可以使用以下变量:

     

...

     

$ entry-在“更新”操作中,当前条目被修改(实际值);

一种实现此目的的方法是使用自定义字段并利用该export class CompanyInvoicesComponent implements OnInit { public isLoading: boolean; public limit = 10; public temp = []; public selected = []; public invoice: Invoice; public statusFilter: number = 0; public rows: Invoice[]; @ViewChild(DatatableComponent) table: DatatableComponent; constructor( private _appService: AppService, private _invoiceService: InvoiceService, ) { this._invoiceService.companyInvoices() .subscribe( (invoices) => { const invoicesArr = []; for (const invoice of invoices) { invoicesArr.push(new Invoice(invoice, true)); } this.rows = invoicesArr; this.temp = [...invoicesArr]; this._appService.toggleLoading(false); }); } updateFilter(event) { const val = event.target.value.toLowerCase(); let temp = this.temp.filter((invoice) => { // the type number is lost after value is changed. const parsedStatusFilter = parseInt(this.statusFilter.toString(), 10); console.log(parsedStatusFilter); if (parsedStatusFilter == 0) { return (invoice.name.toLowerCase().indexOf(val) !== -1 || !val); } else { return (parsedStatusFilter == invoice.statusNumber) && (invoice.name.toLowerCase().indexOf(val) !== -1 || !val); } }); // update the rows this.rows = temp; // Whenever the filter changes, always go back to the first page this.table.offset = 0; } 变量。例如,您可以创建2个这样的自定义字段:

field1.blade.php

$entry

field2.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

然后,在您的控制器中,您总是要添加两个字段,并让这些字段自己进行隐藏适当字段的工作。

@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