I'm using backpack for laravel and I'm trying to add/update some extra columns in a pivot table used in a many-to-many relationship.
Summarizing the context: I have a model Task, another model Machine and this intermediate pivot table machine_task containing the many-to-many relation between Task and Machine
In this machine_task there are machine_id, task_id and then boolean columns m (for 'monthly'), q (for 'quarterly'), b (for 'biannual') and y (for 'yearly').
This is what I have
In my Models/Task.php I've defined the m-2-m relationship
public function machines()
{
return $this->belongsToMany('App\Machine')->withPivot('m','q','b','y');
}
In /app/Http/Controllers/Admin/TaskCrudController.php I have the fields, the most relevant one being this
$this->crud->addField([ // n-n relationship
'label' => "Machines", // Table column heading
'type' => "select2_from_ajax_multiple_custom", // a customized field type modifying the standard select2_from_ajax_multiple type
'name' => 'machines', // the column that contains the ID of that connected entity
'entity' => 'machines', // the method that defines the relationship in your Model
'attribute' => "name", // foreign key attribute that is shown to user
'model' => "App\Models\Machine", // foreign key model
'data_source' => url("api/machines"), // url to controller search function (with /{id} should return model)
'placeholder' => "Select machine(s)",
'minimum_input_length' => 0,
'pivot' => true,
'dependencies' => ['building_id'], // this "Machines" field depends on another previous field value
]);
This works perfectly: When I create or update a Task, the AJAX call is made returning the right results, values are correctly added into the select2 input, and the pivot table machine_task is correctly filled and updated with task_id and machine_id when I click on the Save and back button.
But how to insert into the pivot table the extra values m,q,b,y alongside task_id and machine_id?
At the end of TaskCrudController.php I have
public function store(StoreRequest $request)
{
// your additional operations before save here
// What I should do here to get the pivot values into the request??????????????
$redirect_location = parent::storeCrud($request);
// your additional operations after save here
// use $this->data['entry'] or $this->crud->entry
return $redirect_location;
}
public function update(UpdateRequest $request)
{
// your additional operations before save here
// What I should do here to get the pivot values into the request??????????????
$redirect_location = parent::updateCrud($request);
// your additional operations after save here
// use $this->data['entry'] or $this->crud->entry
return $redirect_location;
}
In my modified version of select2_from_ajax_multiple I have added some rows with checkboxes for each of the options selected. Let's see in a screenshot for better understanding
select2_from_ajax_multiple_custom
In /vendor/backpack/crud/src/resources/views/fields/select2_from_ajax_multiple_custom.blade.php I initialize the values like this, and then I use jquery to update the rows synced with the select2 control, but I don't know how to associate the m,q,b,y checkboxes with each of the select2 selected options and to pass them to the request.
@if ($old_value)
@foreach ($old_value as $item)
@if (!is_object($item))
@php
$item = $connected_entity->find($item);
@endphp
@endif
<div id="div{{ $item->getKey() }}">
<span> {{ $item->getKey() }} {{ $item->{$field['attribute']} }} -- </span>
Monthly <input type="checkbox" id="m{{ $item->getKey() }}" name="m{{ $item->getKey() }}" value="1" @php if ($item->pivot['m'] == "1") echo "checked"; @endphp >
Quarterly <input type="checkbox" id="q{{ $item->getKey() }}" name="q{{ $item->getKey() }}" value="1" @php if ($item->pivot['q'] == "1") echo "checked"; @endphp>
Biannual <input type="checkbox" id="b{{ $item->getKey() }}" name="b{{ $item->getKey() }}" value="1" @php if ($item->pivot['b'] == "1") echo "checked"; @endphp>
Yearly <input type="checkbox" id="y{{ $item->getKey() }}" name="y{{ $item->getKey() }}"value="1" @php if ($item->pivot['y'] == "1") echo "checked"; @endphp> <br/>
@php
@endphp
</div>
@endforeach
@endif
Thank you very much in advance for your time and I hope you can help me! Kinda stuck with this!