我无法找到问题出在哪里。我有posts
和comments
表。在comments
表中我加了foreign key
post_id
。现在,当我收到所有帖子时,我想要同时收到评论。调用未定义的方法App Post :: withCount()
这里是我的PostController
public function show_api()
{
return $this->withCount('comments')->get();
}
public function comments()
{
return $this->hasManyThrough(Comment::class);
}
在CommentController
public function post()
{
return $this->belongsTo(Post::class);
}
哪里是我的错?
你有没有在控制器中定义这些关系?而不是在模型中? –
是的只是在控制器 – MRustamzade
@MRustamzade你也使用'hasManyThrough'这不会在这里工作。 –