Sounds like your wanting to receive the number of comments user_id 10 has made?
Using the ->sum() function runs SQLs sum function on the column, which I'm assuming the comment column is a text type, thus running sum won't really do much.
To get a count of the comments a specific user has made on a post
$post->comments()->where('user_id', 10)->count();
iWader said:
Sounds like your wanting to receive the number of comments user_id 10 has made?
Using the ->sum() function runs SQLs sum function on the column, which I'm assuming the comment column is a text type, thus running sum won't really do much.
To get a count of the comments a specific user has made on a post
$post->comments()->where('user_id', 10)->count();
Hi IWader, thank for replaying. In this contex, let's asume the comments is integer. I'm wondering in this $post = Post::has('comments')->first();
I have a foreach to showing some row of post. it looks like this :
@foreach ($theposts as $thepost)
<?php $num=$number++; ?>
$post = Post::has('comments')->first();
$comment = $post->comments()->where('user_id'=10)->sum(comment);
{{$num}}. | {{$comment }}
@endforeach
but the result will be
It won't sum the next row, that's the problem
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community