laravelista / comments

Native comments for your Laravel application.
MIT License
744 stars 142 forks source link

Comment count #150

Closed eduardonwa closed 3 years ago

eduardonwa commented 3 years ago

I been trying to do this for days, how do I count comments without repeating the count for the other posts?

@foreach ($comments->unique('commentable_id') as $comment)
                            <a class="post-title" 
                                href="/posts/{{ $comment->commentable->slug }}"> 
                                    {{ $comment->commentable->title }}
                            </a>

                            <p class="post-comments">
                                {{ $post->comments->count() }}
                            </p>

                            <span class="action-btn">
                                <a class="post-title" 
                                    href="/posts/{{ $comment->commentable->slug }}#comments"> 
                                    <button class="view-btn" id="listModal"> 
                                        <i class="fas fa-eye"></i> 
                                        List
                                    </button>
                                </a>
                            </span>
                        @endforeach

I have 3 posts but when I apply that in my view I get the count for just 1 of the posts, then the number gets repeated for other posts. I checked this https://github.com/laravelista/comments/issues/149, and that could work as well only problem is still returning the same count for all of my posts.

mabasic commented 3 years ago

You should not loop on comments but on posts and then display the number of comments for that post, not the other way around:

foreach($posts as $post)
    $post->title etc.
    $post->comments->count()

That should do it.

eduardonwa commented 3 years ago

Thank you