Unsure how to handle data from multiple select within a Repeater to save to pivot table.
I'm brand new to Filament and trying to use v3.x in a Laravel 11 app. I have an issue when using a multiselect in a Repeater.
THE SETUP
I've created an example repo (https://github.com/39digits/filament-repeater-multi) as a slimmed down version of the issue and created contrived models to try represent the issue in as simple a way as possible.
In this example repo we have Supplier, Product, and Variant models for the main data.
There is also a SupplierProductVariant pivot model with supplier_id, product_id, and variant_id referencing the other models.
A set number of common Variants exist which can be found across Products. As such there is a many-to-many relationship between the Product and Variants to create a unique list of Product Variants for each Product.
For example, maybe there are Variants such as Small, Medium, Large.
Product A has variants of Small and Large.
Product B has variants of Medium and Large.
Product C has variants of only Small, etc, etc
Separately I have created a Supplier Resource.
One Supplier might only have Product A and only stock the Small variant. While another Supplier might have Product A in Small and Large, and Product B in only Medium. When creating or editing a Supplier I wanted to use a Repeater to add Products + Variant combinations. When a specific Product is selected in the select then the Variant select is populated with only the variants available for that product. So far so good. This works perfectly if both Product and Variant are single selects. However, for ease of use I would like to select a Product and then select multiple variants applicable for that Supplier. I know there is a suggestion to use a JSON field with Repeater data, but I wanted to save the supplier_id, product_id, and variant_id in separate columns against the SupplierProductVariant model to make querying, joins and filtering easier on the front-end. THE ISSUE Changing the Variant select to a multi-select causes errors upon save since we are now dealing with an array. I was hoping I could create a new array where I split the multi-select variant data out and add the product_id to each to then save as separate rows in the pivot table.
Or I thought maybe it would just send the data over to the handleRecordUpdate / handleRecordCreate functions but this is not the case. It seems that the Repeaters try to save before handing over to the Create or Edit page classes and this throws an error. And it is at this point that I have no idea how to handle the multiselect data. I've tried a lot of different things but have no idea how to resolve this. How do I fix this? Or is there a much better way to solve this in Filament? HOW TO REPLICATE Clone the repo (https://github.com/39digits/filament-repeater-multi) and run This will create the tables and add some dummy data along with a very secure user ([email protected] / password). Open
One Supplier might only have Product A and only stock the Small variant. While another Supplier might have Product A in Small and Large, and Product B in only Medium. When creating or editing a Supplier I wanted to use a Repeater to add Products + Variant combinations. When a specific Product is selected in the select then the Variant select is populated with only the variants available for that product. So far so good. This works perfectly if both Product and Variant are single selects. However, for ease of use I would like to select a Product and then select multiple variants applicable for that Supplier. I know there is a suggestion to use a JSON field with Repeater data, but I wanted to save the supplier_id, product_id, and variant_id in separate columns against the SupplierProductVariant model to make querying, joins and filtering easier on the front-end. THE ISSUE Changing the Variant select to a multi-select causes errors upon save since we are now dealing with an array. I was hoping I could create a new array where I split the multi-select variant data out and add the product_id to each to then save as separate rows in the pivot table.
Or I thought maybe it would just send the data over to the handleRecordUpdate / handleRecordCreate functions but this is not the case. It seems that the Repeaters try to save before handing over to the Create or Edit page classes and this throws an error. And it is at this point that I have no idea how to handle the multiselect data. I've tried a lot of different things but have no idea how to resolve this. How do I fix this? Or is there a much better way to solve this in Filament? HOW TO REPLICATE Clone the repo (https://github.com/39digits/filament-repeater-multi) and run This will create the tables and add some dummy data along with a very secure user ([email protected] / password). Open
/admin/suppliers
and edit or create.
Add a Supplier product variant and save. It works as it currently only does single select.
Open app\Filament\Resources\SupplierResource.php
and uncomment the chained call to multiple()
on line 59.
Reload and try create a new Supplier.
You will get an error similar to the below because of the multi-select array.
Any and all help will be greatly appreciated.GitHub
GitHub - 39digits/filament-repeater-multi: Contrived example of an ...
Contrived example of an issue using multiselect field in a Filament Repeater - 39digits/filament-repeater-multi
0 Replies