Я решил это с помощью некоторой пользовательской логики. В результате:
Скриншот моего / admin / product / 1 / edit
Прежде всего, я создал настраиваемое поле:
<!-- /resources/views/vendor/backpack/crud/fields/product_macronutrients.blade.php -->
<!-- product_macronutrients -->
@php($macronutrients = isset($entry) ? $entry->macronutrients : false)
<div @include('crud::inc.field_wrapper_attributes') >
@include('crud::inc.field_translatable_icon')
<div class="array-container form-group">
<table class="table table-bordered table-striped m-b-0">
<thead>
<tr>
<th class="text-center">{{-- <i class="fa fa-trash"></i>--}} </th>
@foreach( $field['columns'] as $column )
<th style="font-weight: 300!important;">
// l10n strings (productscatalog::labels.proteins, productscatalog::labels.fats and so on)
@lang("productscatalog::labels.$column")
</th>
@endforeach
</tr>
</thead>
<tbody ui-sortable="sortableOptions" class="table-striped">
<tr class="array-row">
<td>
<p><b>@lang("productscatalog::labels.macrontr")</b></p>
</td>
@foreach( $field['columns'] as $column)
<td>
<input
class="form-control input-sm"
type="text"
name="{{ $column }}"
value="{{ old($column) ? old($column) : $macronutrients ? $macronutrients->$column : '' }}"
@include('crud::inc.field_attributes')
/>
</td>
@endforeach
</tr>
</tbody>
</table>
</div>
</div>
А ProductCrudController
:
public function setup()
{
// other stuff...
$this->crud->addField([
'label' => 'Macronutrients',
'type' => 'product_macronutrients',
'name' => '',
'columns' => [
'proteins',
'fats',
'carbons',
'calories',
],
]);
}
public function store(StoreRequest $request)
{
$redirect_location = parent::storeCrud($request);
$this->storeOrUpdateMacronutrients($request, $this->crud->entry);
return $redirect_location;
}
public function update(UpdateRequest $request)
{
$redirect_location = parent::updateCrud($request);
$this->storeOrUpdateMacronutrients($request, $this->crud->entry);
return $redirect_location;
}
public function destroy($id)
{
$this->destroyMacronutrients($id);
$return = parent::destroy($id);
return $return;
}
protected function storeOrUpdateMacronutrients(Request $request, Product $product)
{
$macronutrients = Macronutrients::firstOrNew(['id' => $product->id]);
$macronutrients->proteins = $request->input('proteins');
$macronutrients->fats = $request->input('fats');
$macronutrients->carbons = $request->input('carbons');
$macronutrients->calories = $request->input('calories');
$macronutrients->save();
}
protected function destroyMacronutrients($productId)
{
$macronutrients = Macronutrients::findOrFail($productId);
$macronutrients->delete();
}
Надеюсь, это поможет.