【发布时间】:2018-04-13 09:55:08
【问题描述】:
我使用的是 laravel 5.5.13。
我有App\Entity,其中有很多App\Comment 和很多App\Thumb。
现在我可以像这样轻松获取评论和拇指:
public function show(Entity $entity)
{
return $entity->load('comments')->load('thumbs');
}
这给出了这样的数据:
{
"id": 1,
"kind": null,
"name": "three",
"created_at": "2017-11-01 04:29:22",
"updated_at": "2017-11-01 04:29:22",
"comments": [
{
"id": 5,
"body": "no non o",
"displayname_id": 4,
"entity_id": 1,
"created_at": "2017-11-01 05:16:14",
"updated_at": "2017-11-01 05:16:14"
}
],
"thumbs": [
{
"id": 9,
"like": 0,
"displayname_id": 5,
"entity_id": 1,
"created_at": "2017-11-01 05:16:39",
"updated_at": "2017-11-01 05:16:39"
}
]
}
然而,我还需要$comment->displaynames() 和$thumb->displaynames() 的列表,并且每个评论都有许多App\Votes 通过$comment->votes()。可能吗。我读了很多关于枢轴的文章,但我真的很困惑。
我的目标是得到这样的数据:
{
// removed for concise (same as above)
"comments": [
{
// removed for concise (same as above)
"votes": [
..... // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< I want to get this
]
},
{
// removed for concise (same as above)
"votes": [
..... // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< I want to get this
]
}
],
"thumbs": [
{
// removed for concise (same as above)
}
],
"displaynames": [
..... // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< I want to get this
]
}
我们在这里看到了新的displaynames 数组和每个comment 中的votes 数组。
我是否必须在此处使用枢轴来建立一对多关系?
【问题讨论】:
标签: laravel-5.5