What would be the proper way to decode this json?
my model has
protected $casts = [
'items' => 'array'
];
my json items:
{
"data": [
{
"name": "Google",
"link": "http://google.com"
},
{
"name": "ALink",
"link": "http://link.org"
}
]
}
json_decode($request->items)
returns the error: ErrorException: Object of class stdClass could not be converted to string in file
Your json is of an object, not an array. You should cast it to an object like this
protected $casts = [
'items' => 'object'
];
To save the json into your model you can then do $obj->items = $request->items
after changing the cast to object as above.
Alternatively, If you have to save this as an array then you can cast the request into one by doing (array)$request->items
instead of json_decode.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.