Saving repeater with relationship as json

Is it possible to save a repeater's data as json when the repeater has a relationship? To better explain, I am working within the panel builder. I have 2 main models, Order and Document. Order hasMany documents and Document belongsTo Order. Inside my OrderResource file: If I go ahead and setup a repeater named ('form_data') with no relationship, and then I set my orders table properly with a json data type column and my model with the proper casts, the data gets saved correctly into the form_data column as a json object. But then if I make a change to the repeater and add ->relationship('documents'), keep the same name for the repeater ('form_data'), then when I try to save the data it tries to save the data in individual columns based on the names of the fields inside the repeater as opposed to saving them inside the column 'form_data' under my documents table. I have looked at the filament demo along with multiple videos on laracasts, codecourse, youtube, and I am yet to find an example of someone saving a repeater's data with a relationship inside a json column. So perhaps I am just trying to do something that is not actually supported. Happy to paste any code if this is something that is supported.
4 Replies
awcodes
awcodes11mo ago
It’s not natively supported because it’s an anti pattern to save a relationship as json. You loose the benefits of the relationship. But, you can override the mutateFormDataBeforeSave and mutateFormDataBeforeCreate to change the data in any way you need. The main problem with what you are trying to do is that the data can easily get out of sync.
rg.block
rg.blockOP11mo ago
I see. My main issue is the amount of fields I have inside the repeater along with some conditional logic that shows different tabs depending on the value of a select. To explain it a different way, each one of my documents can have different supporting data (form_data) based on the type of document it is, I then render different tabs within the repeated based on that type, and ideally I wanted to save that data as json on a column inside my documents table. Initially I thought about having a table for each one of the forms, and have each table have columns that match each of the fields on the form, I am just trying to avoid that since I am not really going to be querying the data individually and I need is to present it when viewing the record. I did managed to get it working using mutate, but then the issue was on the edit portion trying to make it fill the forms inside the tabs.
awcodes
awcodes11mo ago
There’s also mutateFormDataBeforeFill() 🙂 Understandable, but the form should never dictate the data. It should be the other way around. If you have a complex form the just get it done. Don’t go against best practices or convention to make that one part of the job less tedious. Just my 2 cents though.
rg.block
rg.blockOP11mo ago
Yes, appreciate that reminder. I think I just carried along this way because I was looking at the structure wrong. I was not seeing this as trying to save the documents relationship as json but instead just trying to save the form data inside a field within the relationship just like I would save the document name in the name column of my documents table if I had Textinput::make('name').
Want results from more Discord servers?
Add your server