我在 Laravel 7.x 上,我有两个具有父子关系的模型(CustomerOrder
由许多模型组成)。CustomerOrderLines
Parent ( 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 字段格式不正确。似乎它没有通过 Resource 类。请告诉我,我怎样才能解决这个问题?
供参考
CustomerOrderLine.php:
public function parent()
{
return $this->belongsTo(CustomerOrder::class);
}