首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel: json field的资源

Laravel: json field的资源
EN

Stack Overflow用户
提问于 2020-05-27 04:07:43
回答 1查看 773关注 0票数 0

我在拉拉维尔7.x,我有两种模式(CustomerOrder由许多CustomerOrderLines组成)与父子关系。父(CustomerOrder)模型的字段中有一个json类型字段。

CustomerOrderResource.php:

代码语言:javascript
运行
复制
return [
  'id' => $this->id,
  'wfx_oc_no' => $this->wfx_oc_no,
  'qty_json' => json_decode($this->qty_json)
];

CustomerOrderLineResource.php:

代码语言:javascript
运行
复制
return [
  'id' => $this->id,
  'description' => $this->description,
  'customer-order' => $this->customerOrder
];

CustomerOrder->GET请求返回格式正确的数据如下:

代码语言:javascript
运行
复制
"data": {
    "id": 11,
    "wfx_oc_no": 12,
    "qty_json": {
        "L": "20",
        "M": "30",
        "S": "20",
        "XL": "100"
    }
}

但是对于CustomerOrderLine->GET,响应如下:

代码语言:javascript
运行
复制
"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:

代码语言:javascript
运行
复制
public function parent()
{
   return $this->belongsTo(CustomerOrder::class);
}
EN

回答 1

Stack Overflow用户

发布于 2020-05-27 10:04:02

最后用json铸造解决了这个问题。该字段包含在模型中的$casts数组中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62035269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档