我正在尝试使用以下代码转换故障单的集合
public function transform(Ticket $ticket) {
return [
'id' => $ticket->id,
'title' => $ticket->title,
'status' => $this->transformerMessage($ticket->status),
'interactions' =>
$this->collection(
$ticket->interactions,
new InteractionTransformer(),
'interactions'
)
];
}
但互动结果始终为空。这是我得到的输出示例:
{
"data": [
{
"id": 1,
"title": "Earum repudiandae corporis sapiente at odit itaque ratione.",
"status": "Open",
"interactions": {}
},
{
"id": 2,
"title": "Odit impedit vitae quo sit molestiae eius.",
"status": "Open",
"interactions": {}
},
{
"id": 3,
"title": "Fuga cum corrupti ut.",
"status": "Open",
"interactions": {}
}
]
}
如果我尝试消亡并转储 $ticket->interactions
变量,我会得到5个项目的集合(证明该变量不为空)。
我在这里做错了什么?
感谢您的关注。
答案 0 :(得分:0)
你的集合调用中的$ this指的是从TransformerAbstract继承的变换器,它从资源类型创建一个集合。
尝试添加 - > getData()到最后,从Dingo资源集合中提取REAL集合。
public function transform(Ticket $ticket) {
return [
'id' => $ticket->id,
'title' => $ticket->title,
'status' => $this->transformerMessage($ticket->status),
'interactions' =>
$this->collection(
$ticket->interactions,
new InteractionTransformer(),
'interactions'
)->getData()
];
}
答案 1 :(得分:0)
试试这个。回调引用dingo变换:
public function transform(Ticket $ticket) {
$output = $ticket->interactions;
$trans = new InteractionTransformer();
return [
'id' => $ticket->id,
'title' => $ticket->title,
'status' => $this->transformerMessage($ticket->status),
'interactions' =>
$ticket->interactions->transform(
function($i) use ($trans) {
return $trans->transform($i);
}
)
];
}