嘿,我正在制作数据表以显示登录并发布该帖子的用户对我的帖子发表了多少评论,但我遇到了一些错误,它只检索了 1 条,但实际上我收到了 26 条评论,我不知道为什么。
在我的 Controller 中:
public function getCountComment()
{
$user = Auth::user();
return $all_count = $user->post()
->withCount('comment_to_post')
->take(5)->get();
}
模型comment.php:
public function comment_to_post()
{
return $this->belongsTo('App\Post','id_user');
}
我有大约 26 条评论,但它只检索到 1 条评论。希望大家能帮帮我
"comment_to_post_count": 1
最佳答案
我已经在我的代码中这样做了 - 希望它有帮助
public function getCountComment()
{
$user = Auth::user();
$posts = $user->posts();
foreach ($posts as $key => $value) {
$posts[$key]->post_comments_count = PostComment::where('post_id', $value->id)->count();
}
return $posts;
}
关于mysql - 如何从不同的表中计算laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52399440/