Laravel dingoApi 的 Transformers 能否嵌套,用里一个表?

老师表teacher 对老师的评论表comment
return $this->response->collection($teacher, new TeacherInfoTransformer) 这个返回格式
data:[
{
"name":zhangsan,
"country": null,
"city": null,
"address": null,
}
]

现在想把评论也加进去,应该如何做呢。
比如得到如下格式:
data:[
{
"name":zhangsan,
"country": null,
"city": null,
"address": null,
“comment”:[
{
"score":5,
"content":“very good”
},
{
"score":4
"content":“very bad”
},
]
}
]

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 3

可以使用 Including Data 。 具体参考文档 Transformers

6年前 评论

Including Data 正解,这也是 Dingo 最为方便的一个地方,不过 dingo 至今都有个坑未填上,你可能也会遇到。博客:从 Dingo API 原理来看 Laravel 的 Http 请求处理过程

6年前 评论
朕略显ぼうっと萌

难道不能这样写吗?

/**
 * @param $model
 * @return array
 */
public function transformData($model)
{
    return [
        'id' => $model->id,
        'family' => $this->familyTransFormData($model->family), 
        'sub_date' => $model->subscribe_time, 
        'order' => $this->orderTransFormData($model->order), 
        'status' => $model->status,
        'type' => $model->type, 
    ];
}

在我理解 这就是一个方法, 方法里面怎么写不行啊

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!