Reputation: 5679
Can I setup CRUD Controller in the way to show fields depending on model is editing?
Example: I have model with fields: id
, type
, field1
, field2
.
For models with type=type1
I want to show only field1
:
$this->crud->addFields([
['name' => 'field1', 'label' => 'field1 label']
]);
for models with type=type2
only field2
:
$this->crud->addFields([
['name' => 'field2', 'label' => 'field2 label']
]);
for models with type=type3
both field1
and field2
:
$this->crud->addFields([
['name' => 'field1', 'label' => 'field1 label'],
['name' => 'field2', 'label' => 'field2 label']
]);
Upvotes: 2
Views: 1343
Reputation: 19571
At the very bottom of this page in the docs it lists:
Inside your custom field type, you can use these variables:
...
$entry - in the Update operation, the current entry being modified (the actual values);
One way to achieve this would be to use custom fields and leverage that $entry
variable. For example, you could make 2 custom fields like this:
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
Then, in your controller, you'd always add both fields and let the fields themselves do the work of hiding the proper ones.
$this->crud->addFields([
[
'name' => 'field1',
'label' => 'field1 label',
'type' => 'field1',
],
[
'name' => 'field2',
'label' => 'field2 label',
'type' => 'field2',
]
]);
Upvotes: 2