Handling repeater items BelongsToMany select field manually
Background
Please find my current code attached.
I in my ProductResource, I have a repeater form field for the HasMany relationship between my Product model and my ProductSku model. Then in my repeater items, I have a
price
, a sku
, and a attribute_values_id
field. The attribute_values_id
is a select field with a BelongsToMany relationship between ProductSku and AttributeValue. This all works great, but I set the attribute_values_id
from an attribute_id
field outside of the repeater, meaning that I build all the necessary repeater items for a product, based on the selected attribute_id
.
Why
Since I only use my attribute_values_id
select field to store data in the pivot table between ProductSku and AttributeValue, and to set the ItemLabel, this field just causes the product creation process to be less intuitive. So I need to find a way to keep the existing functionality, without showing the attribute_values_id
select field.
What I Have tried
Since everything works functionally, just being able to hide the select field would solve my problem. But if I add ->hidden()
to the select field, no pivot table data is stored in the database when I submit the form.
I also tried to use a Hidden field: Forms\Components\Hidden::make('attribute_values_id'),
, and then use a ->mutateRelationshipDataBeforeCreateUsing()
to somehow establish the BelongsToMany relationship, but I am not sure I can set relationship data for a ProductSku model that is not yet created.
What I need
I need to find a recommended approach for storing the BelongsToMany relationship between ProductSku and AttributeValue, without showing/using a BelongsToMany Select field inside the ProductSku Repeater field.1 Reply
I just noticed that my attached code is not functional due to the select field being disabled. So for a working setup I need the select field to be interactive as well, making it even more important for me to be able to not show the field, or use a manual approach for the relationship handling.
This has been solved by an answer to my other question here: https://discord.com/channels/883083792112300104/1215763167775428822
I found out that saveRelationshipsWhenHidden() existed, now I can simply do this to keep my existing Select field hidden without loosing functionality.