Support the ongoing development of Laravel.io →
Database Eloquent Queues
Last updated 2 years ago.
0

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();
Last updated 2 years ago.
0

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

  1. | 4
  2. | 0
  3. | 0 ... and so on

It won't sum the next row, that's the problem

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.