Laravel 如何加载与{json:api}客户端的关系?
我正在使用语法将json解析为雄辩的模型 我有两种型号,Laravel 如何加载与{json:api}客户端的关系?,laravel,json-api,Laravel,Json Api,我正在使用语法将json解析为雄辩的模型 我有两种型号,Congress和Speaker。一个大会有许多演讲者 这就是我能够做到的: $repository = app(App\Repositories\CongressRepository::class); $document = $repository->all(); $document是具有以下属性的集合文档: 我想请第一届大会的演讲者。这就是我试过的 $congresses = $document->getData();
Congress
和Speaker
。一个大会
有许多演讲者
这就是我能够做到的:
$repository = app(App\Repositories\CongressRepository::class);
$document = $repository->all();
$document
是具有以下属性的集合文档
:
我想请第一届大会的演讲者
。这就是我试过的
$congresses = $document->getData();
$congress = $congresses->first();
$congress->speakers // <- this is null!
$congressions=$document->getData();
$congressions=$congressions->first();
$congress->speakers//speakers
null?我还试着告诉我们$repository->all(['includes'=>'speakers'])代码>
但这没有区别 似乎$congress->speakers
为空,因为关系为空:
我使用包创建json
输出。在Schema.php
中,我必须添加self::DATA
以使数据可见,如中所述
我仍然想知道,如果API
中只给出了链接,是否可以加载该关系
public function getRelationships($resource, $isPrimary, array $includeRelationships)
{
return [
'speakers' => [
self::SHOW_SELF => true,
self::SHOW_RELATED => true,
self::DATA => function () use ($resource) {
return $resource->speakers;
},
],
];
}