How do I prefill relationship pivot field? #1354
Replies: 1 comment
-
|
To prefill a relationship field with pivot data in Backpack, you need to ensure that the value you provide matches the expected structure for the relationship field, especially when using subfields and a pivotSelect. From your code, you are correctly providing an array of values for the field, including the pivot attributes (quantity, notes). However, the main select (the product itself) is not being preselected. This is a common issue, and the solution is to ensure that the value array includes the correct key for the related model's primary key, which is usually product_id (not just id), depending on your relationship and field configuration. For a BelongsToMany relationship with subfields, Backpack expects the value array to look like this: 'value' => [
[
'product_id' => 1, // The foreign key column name, not just 'id'
'quantity' => 50,
'notes' => 'Test note',
],
],If your relationship is named products and the related model's primary key is id, Backpack usually expects product_id as the key in the value array. If you use just id, it may not match the expected input and thus not preselect the product in the pivotSelect. Key points:
Example: $this->crud->addField([
'name' => 'products',
'label' => 'Products label',
'type' => 'relationship',
'new_item_label' => 'Add product',
'value' => [
[
'product_id' => 1, // Use the correct foreign key
'quantity' => 50,
'notes' => 'Test note',
],
],
'subfields' => [
[
'name' => 'quantity',
'type' => 'number',
'label' => 'quantity',
'wrapper' => ['class' => 'form-group col-md-3'],
'default' => 1,
'attributes' => ["step" => "0.01"],
],
[
'name' => 'notes',
'type' => 'text',
'label' => 'notes',
'wrapper' => ['class' => 'form-group col-md-6'],
],
],
'pivotSelect' => [
'label' => 'Product',
'placeholder' => 'Pick a product',
'wrapper' => ['class' => 'form-group col-md-3'],
'default' => 1,
'ajax' => true,
'attribute' => 'product_name',
'data_source' => backpack_url('product/fetch/product'),
],
]);If you still have issues, double-check the structure of the value array and the foreign key name. Also, ensure your AJAX endpoint returns the correct data for the select. This approach is confirmed in the Backpack documentation and community discussions, where the value array must match the expected keys for the relationship and pivot fields. If you follow this structure and it still doesn't work, there may be a bug or a specific configuration issue in your setup. In that case, reviewing the Backpack documentation on saving additional data to the pivot table and related GitHub issues may help clarify further steps. If you are using AJAX and the product is not being preselected, ensure that the AJAX endpoint can return the selected product as an option, even if it wouldn't normally appear in the search results. This is a common requirement for select2 AJAX fields in Backpack and Laravel in general. References: ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️ Relevant sources: Github Issue (closed): Problem with select2_from_ajax_multiple |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I would like to prefill relationship fields. I will get values from URL, but don't know how to prefill it:
This prefills only quantity and notes. But it does not select the product.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions