F
Filamentβ€’8mo ago
Xane

Is there a way to use statePath with nested json-data?

Hello! I'm wondering if anyone knows if there is any way to use statePath to get nested json properties? DB Field: "Nutrient" JSON: {"Details": [{"NutrientTypeCode":"ENER-","Value1":"1724"}, ...]} I would like to get "NutrientTypeCode". This is what I've got so far, it shows me all the detail objects, but I don't know how to target the field I want: Forms\Components\Section::make('Nutrient')->statePath('Nutrient')->schema([ Forms\Components\TextInput::make('Details'), ]), This is as far I've gotten with it. I've tried "Details.NutrientTypeCode", but this doesn't give me any data. Do I have to create my own form-field for this? Thanks!
4 Replies
toeknee
toekneeβ€’8mo ago
Is this for just rendering? Or render and updating? Essentially you could add it as an attribute on the model, whereby you decode the json data Or you can formatStateUsing(fn($record) => json_decode($record->nutrient)->details)
Xane
Xaneβ€’8mo ago
This is for both rendering and updating. I'm looking into formatStateUsing! Thanks @toeknee! I tried a few thing, and what worked in the end was simply using the Repeater with "->statePath('Nutrient.Details')" πŸ™‚
toeknee
toekneeβ€’8mo ago
Ohh that's interesting πŸ™‚
Xane
Xaneβ€’8mo ago
The reason I didn't see the values before was because I had multiple rows in "Details", when there were no data displayed it confused me thinking that it in fact did not work. But the repeater solved that problem by looping through all the rows in Details πŸ™‚