我在构建论坛API时遇到了麻烦。基本上,我正试图让用户“喜欢”这些帖子。我正在使用多对多的多态关系。 通过spatie使用laravel-fractal。
{
"data": {
"id": 1,
"title": "uus teema",
"slug": "uus-teema",
"body": "teema sisu",
"diffForHumans": "5 minutes ago",
"user": {
"data": {
"username": "ventt",
"avatar": "http://www.gravatar.com/avatar/f89167803d475a4c6309cb888273d9bb?s=35&d=mm"
}
},
"posts": {
"data": [
{
"id": 1,
"body": "joujoujouuuuu wadap boys",
"diffForHumans": "5 minutes ago"
}
]
},
"likes": {
"data": []
}
}
}
问题是,我会在post数组中显示“likes”数组,而不是在它下面。
此方法将返回我之前复制的数组。
public function show(Topic $topic) {
return fractal()
->item($topic)
->includeUser()
->includePosts()
->includeLikes()
->transformWith(new TopicTransformer)
->toArray();
}
这包括喜欢。
public function includeLikes(Topic $topic) {
return $this->collection($topic->likes, new LikeTransformer);
}
我应该如何继续?