Laravel Nova Handling Mysql nested JSON data

Viewed 336

I have a table that contains questions where the question itself and the question quantity can change. Instead of linking the data with FKs, I simply chose to take the question data and save it to a JSON column with the parent record data (the person who answered the questions). The JSON won't drive any analytics or metrics, so I am not concerned about a performance issue. I simply want to create a snapshot of the questions they answered and what they were at the time.

Here is how it is formatted.

[{
    "id": 2,
    "question": "Question 1",
    "response": "no",
    "created_at": "2020-05-19T15:34:09.000000Z",
    "updated_at": "2020-05-19T15:34:09.000000Z"
}, {
    "id": 3,
    "question": "Question 2",
    "response": "no",
    "created_at": "2020-05-19T15:39:28.000000Z",
    "updated_at": "2020-05-19T15:39:28.000000Z"
}, {
    "id": 4,
    "question": "Question 3",
    "response": "no",
    "created_at": "2020-05-19T15:39:38.000000Z",
    "updated_at": "2020-05-19T15:53:55.000000Z"
}]

When I try to display the data using KeyValue::make('Meta') it puts a single character in key and value column for the entire json, creates thousands of 'pairs' Key=> T, Value=> H, on and on.

However, I can use...

KeyValue::make('Meta', 'meta.0')
KeyValue::make('Meta', 'meta.1')
KeyValue::make('Meta', 'meta.3')

Unfortunately this approach assumes I know how many questions they answered, I don't. If I simply put more than I expect to have, it creates extra 'Meta' placeholders with no data.

It's usable but I am assuming there is a way to count the JSON records and create the Return based on how many records it has.

0 Answers
Related