Setting table columns based on model's json column
I have this structure:
Form
Response
a form has n responses, 1 response belongs to 1 form
a form has a json field called fields - example:
a response has a field called responses - example:
in the form's responses table, i want to set the table's columns dynamically, so it shows all form fields and their responses. im trying this:
(file
app/Filament/Resources/ResponseResource.php
)
which results into this image here, where the birthdate isnt shown. why??? if i just dd $record->responses[str($field['name'])->slug()->toString()]
in my tinker, it works, it shows -> "2023-11-25"
7 Replies
if something is unclear, let me know please, so i can try to explain it better
have you tried
dd()
inside the ->formatStateUsing()
?yes, it doesnt dd
but if i instead do it in here, when mergin the created_at field, it dds
Try using getStateUsing instead of formatStateUsing, it won't call if the column name does not exist on the base data
'birthdate' doesn't exist on $record so it won't even attempt to call the format function as there isn't really anything to format.
cant believe it
works
u think the code looks fine?
or is kinda weird?
i mean, u'd do it the way i did it?
just looking for some feedback now ( : , if u dont mind
Nah I don't see anything inherently wrong with it, you could maybe store the slug as part of the fields def so you don't have to repeat this:
Just $field['slug'] or something
thanks (: appreciate ur help y'all