F
Filament5mo ago
dasK_

Converting form column to JSON?

Having some issues with filling a column out called productDetails, it's set up to accept json and I've been doing it fine with straight laravel by means of json_encode to write and json_decode to read but in Filament, the only thing I'm able to do is write a string to the productDetails column when I edit a product, creating a product it gives me an Array to string conversion exception, I've tried to use the save function to do this (chatGPT may have this wrong because I'm not getting any logs) Hopefully the code will make more sense, I've also provided an example of what it's meant to look like in the DB. https://gist.github.com/tommmoe/5a33c7057d5081f985cb74fb08934d9d#file-gistfile1-txt
Gist
json fields
json fields. GitHub Gist: instantly share code, notes, and snippets.
Solution:
That's how it's encoded but when you decode it it should be correct? Are the items part of a repeater? Then. would make them as you want them I believe
Jump to solution
5 Replies
dasK_
dasK_5mo ago
bump!
toeknee
toeknee5mo ago
In your model, ensure the column is cast to array procted $casts = ['my_column' => array];
dasK_
dasK_5mo ago
Thanks for the reply, tried that.. it does make it an 'array' but it's just a flat json. I'm looking to get something like this '[ { "name": "PLAN_DETAILS[PLAN_TYPE]", "value": "test1" }, { "name": "PLAN_DETAILS[NETWORK]", "value": "test" }, { "name": "PLAN_DETAILS[ACTIVATION_POLICY]", "value": "test" }, { "name": "PLAN_DETAILS[eKYC]", "value": "test" }, { "name": "PLAN_DETAILS[TOP_UP_OPTION]", "value": "test" }, { "name": "PLAN_TITLE", "value": "Australia 1 GB" }, { "name": "PLAN_VALIDITY", "value": "720" }, { "name": "PLAN_DATA_UNIT", "value": "GB" }, { "name": "TAGS", "value": null }, { "name": "FIVEG", "value": "0" }, { "name": "PLAN_DATA_LIMIT", "value": "1" }, { "name": "EXTERNALLY_SHOWN", "value": "1" } ]' but this is what i get { "TAGS": "test", "FIVEG": "test", "PLAN_TITLE": "test", "PLAN_VALIDITY": "test", "PLAN_DATA_UNIT": "test", "PLAN_DATA_LIMIT": "test", "EXTERNALLY_SHOWN": "test", "PLAN_DETAILS[eKYC]": "test", "PLAN_DETAILS[NETWORK]": "test", "PLAN_DETAILS[PLAN_TYPE]": "test", "PLAN_DETAILS[TOP_UP_OPTION]": "test", "PLAN_DETAILS[ACTIVATION_POLICY]": "test" } I was able to just json_encode it in laravel, but not sure how to do it in filament/livewire?
Solution
toeknee
toeknee5mo ago
That's how it's encoded but when you decode it it should be correct? Are the items part of a repeater? Then. would make them as you want them I believe
dasK_
dasK_5mo ago
you're correct, my bad. This will do just fine! thank you for the help