Fractal中JsonApiSerializer包含的数据类型为null



这就是我目前在Laravel中使用Fractal的JsonApiSerizer获得的结果:

{
"type": "projects",
"id": "18",
"attributes": {
"name": "pariatur",
"parent_id": 1
},
"relationships": {
"tasks": {
"data": [
{
"type": null,
"id": "245"
}
]
}
}
}
],
"included": [
{
"type": null,
"id": "435",
"attributes": {
"name": "incidunt",
"content": "Laboriosam occaecati qui voluptas suscipit. Magni numquam incidunt pariatur et at. Alias molestias perspiciatis quae sit.",
"project_id": 12,
"due_at": "1971-05-19"
}
},

我知道你可以为请求的资源设置类型,比如:

// Important, notice the Resource Key in the third parameter:
$resource = new Item($book, new JsonApiBookTransformer(), 'books');
$resource = new Collection($books, new JsonApiBookTransformer(), 'books');

如何对包含的数据执行此操作?

对于那些可能偶然发现这一点的人。我忽略了需要在调用collection((以包含数据的相应转换器中设置ressourceKey。当然有道理。就我而言:

public function includeTasks(Project $project, ParamBag $paramBag)
{
list($orderCol, $orderBy) = $paramBag->get('order') ?: ['created_at', 'desc'];
$tasks = $project->tasks()->orderBy($orderCol, $orderBy)->get();
return $this->collection($tasks, App::make(TaskTransformer::class), 'tasks');
}

相关内容

最新更新