Reputation: 151
Hei I'm making data table to show how many comment that on my post from user that login and making that post but I got some bug it retrieve only 1 but actually I got like 26 comment, I dont know why.
In my controller:
public function getCountComment()
{
$user = Auth::user();
return $all_count = $user->post()
->withCount('comment_to_post')
->take(5)->get();
}
Model comment.php:
public function comment_to_post()
{
return $this->belongsTo('App\Post','id_user');
}
I've got like 26 comment but it retrieve only 1 comment count. Hope you can help me guys
"comment_to_post_count": 1
Upvotes: 1
Views: 164
Reputation:
You should try this:
public function getCountComment()
{
$user = Auth::user();
$all_count = $user->post()
->withCount('comment_to_post')
->count();
return $all_count;
}
Upvotes: 1
Reputation: 892
I have done like this in my code - hope it helps
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;
}
Upvotes: 1