我在拉拉维尔7.x,我有两种模式(CustomerOrder
由许多CustomerOrderLines
组成)与父子关系。父(CustomerOrder
)模型的字段中有一个json类型字段。
CustomerOrderResource.php:
return [
'id' => $this->id,
'wfx_oc_no' => $this->wfx_oc_no,
'qty_json' => json_decode($this->qty_json)
];
CustomerOrderLineResource.php:
return [
'id' => $this->id,
'description' => $this->description,
'customer-order' => $this->customerOrder
];
CustomerOrder->GET请求返回格式正确的数据如下:
"data": {
"id": 11,
"wfx_oc_no": 12,
"qty_json": {
"L": "20",
"M": "30",
"S": "20",
"XL": "100"
}
}
但是对于CustomerOrderLine->GET,响应如下:
"data": {
"id": 15,
"description": "test desc",
"customer-order": {
"id": 11,
"wfx_oc_no": 12,
"qty_json": "{\"L\": \"20\", \"M\": \"30\", \"S\": \"20\", \"XL\": \"100\"}"
}
}
json字段格式不正确。它似乎没有通过资源类。请告诉我,我怎样才能把这个修好?
FYI
CustomerOrderLine.php:
public function parent()
{
return $this->belongsTo(CustomerOrder::class);
}
发布于 2020-05-27 10:04:02
最后用json铸造解决了这个问题。该字段包含在模型中的$casts
数组中。
https://stackoverflow.com/questions/62035269
复制相似问题