How do I prevent JSON fields from being blown away when using dot notation on TextInputs?
What I'm trying to do:
I'm trying to store data into a JSON field using a TextInput while using dot notation.
Forms\Components\TextInput::make('additional.industry')
additional
is my JSON field, industry
is the key in that field I'm editing.
My Issue
My JSON field has data other than just the 'industry' key, and it is being set elsewhere.
When I save submit the Filament form, additional.industry
saves what I'm expecting, however every other JSON key is blown away.
Example:
JSON field before saving:
{"other": "other data here", "industry": "service"}
JSON field after saving
{"industry": "service"}
Question
How do I preserve the rest of the JSON structure when saving while using dot notation for a TextInput field?6 Replies
Wrap all the additional fields in a Group->statePath(‘additional’) then just use the key for each input’s name.
I think. Lol.
Thank you for the response. I was looking at doing this, but many of the keys are generated with mostly unique ID's. At this point I would do what you suggested just to move forward, but because I dont know all of the keys in advance I cannot.
Best bet then is to use a lifecycle hook to aggregate all the values you need for the db. But not sure how you even read that back out if your keys are dynamic.
Thank you awcodes. I'm going to keep digging. Will follow up when I find a solution. 🙂
No worries. I could just be misunderstanding your schema too. But it’s sounds off to me for a json field.
Is using a json field (or a sub-key there of) a normal way to store data out of a text field? I may just be misusing the field. Given the dot notation saves properly I assumed this was an accepted practice. What I may end up doing is just creating a second json field. One for all of the dynamic keys and another for the keys I want to save using Filament. Then I'll just do exactly what you suggested as that should work just fine.