Karlis Pokkers
Karlis Pokkers

Reputation: 244

Laravel 5.7 Comment count for different posts

I am stucked at point where i got comment count for posts but it shows count of all comments of all posts on every post. I would like to know how to output in blade comment count for post ID

here is controller:

  $posts = $posts->orderBy("posted_at", "desc")
        ->paginate(config("blogetc.per_page", 10));

    $comments = BlogEtcComment::all();


    return view("blogetc::index", [
        'posts' => $posts,
        'title' => $title,
        'comments' => $comments,
    ]);

blade:

@foreach($posts as $post)
<section class="blog_area p_120">
        <div class="container">
            <div class="row">
                <div class="col-lg-8">
                    <div class="blog_left_sidebar">
                        <article class="blog_style1">
                            <div class="blog_img">
                                <img class="img-fluid" src="blog_images/{{$post->image_large}}" alt="">
                            </div>
                            <div class="blog_text">
                                <div class="blog_text_inner">
                                    <div class="cat">
                                        <a class="cat_btn" href="{{$post->url()}}">{{$post->slug}}</a>
                                        <a href="#"><i class="fa fa-calendar" aria-hidden="true"></i>{{$post->created_at}}</a>
                                        <a href="{{$post->url()}}"><i class="fa fa-comments-o" aria-hidden="true"></i> {{count($comments)}}</a>
                                    </div>
                                    <a href="{{$post->url()}}"><h4>{{$post->title}}</h4></a>
                                    <p>{!! $post->generate_introduction(400) !!}</p>
                                    <a class="blog_btn" href="{{$post->url()}}">Lasīt vairāk</a>
                                </div>
                            </div>
                        </article>
                    </div>
                </div>
            </div>
        </div>
</section>
@endforeach

Upvotes: 1

Views: 1380

Answers (2)

Lawrence Njenga
Lawrence Njenga

Reputation: 326

//Quickfix:

//assuming your posts table is called posts and that in your blogetccomments table //you have a post_id column pointing to the original post. Try something like

$posts = DB::table('posts')
    ->leftJoin('blogetccomments', 'posts.id', '=', 'blogetccomments.post_id')
    ->selectRaw('posts.*, count(blogetccomments.post_id) as commentcount')
    ->groupBy('posts.id')
    ->get();

In your blade template, Access the comments count for each post, as follows..

@foreach($posts as $post)
...
{{$post->title}}...
{{$post->commentcount}}

...
@endforeach

Upvotes: 1

Mihir Bhende
Mihir Bhende

Reputation: 9045

You can use withCount() to get count of comments for a specific post :

$posts = $posts->withCount('comments')
        ->orderBy("posted_at", "desc")
        ->paginate(config("blogetc.per_page", 10));

Above would require you to have comments relation on your Post Model :

Post Model :

public function comments()
{
    return $this->hasMany(BlogEtcComment::class);
}

BlogEtcComment Model :

public function post()
{
    return $this->belongsTo(Post::class);
}

And then in blade :

@foreach($posts as $post)
  <p>Post : $post->id</p>
  <p>Comments : $post->comments_count</p>
@endforeach

Upvotes: 0

Related Questions