Let's say I have a table called Posts with these columns:
-- uid (int)
-- title (str)
-- body (text)
-- data (json)
and in one row, inside the data column, I have a JSON encoded response like so:
[{
"title": "Blue",
"points": 0,
"tags": [{
"type": "Comedy",
"uid": 45
}]
}, {
"title": "Green Orange",
"points": 2,
"tags": [{
"type": "Horror",
"uid": 1
}]
}]
All of that is json_encode into one database column.
How can I make it so that I can get the Green Orange into a variable?
I have tried:
$post = Post::where('id', $id)->first();
$data = json_decode($post->data);
return $data[0];
But that just gives me: The Response content must be a string or object implementing __toString(), \"object\" given.
dd($data)?json_decodeprovides by default\StdClassuse this to retrieve an arrayjson_decode($post->data, true);And access properties with$data[0]['title']$post = Post::find($id);