【问题标题】:Laravel 5.7 Comment count for different postsLaravel 5.7 不同帖子的评论计数
【发布时间】:2019-02-20 08:47:50
【问题描述】:

我被困在我获得帖子评论计数的地方,但它显示了每个帖子上所有帖子的所有 cmets 的计数。我想知道如何输出帖子 ID 的刀片评论计数

这里是控制器:

  $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,
    ]);

刀片:

@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

【问题讨论】:

  • 你需要使用groupBy子句。
  • 我找到了另一种方法,但不知道这是一种好习惯。我将刀片中的“{{count($cmets)}}”更改为“{{count($post->cmets)}}”,它对我有用
  • 更换刀片无济于事,因为您仍然会这样做{{count($comments)}} 和$comments = BlogEtcComment::all(); 将始终包含所有帖子的所有记录。可以展示模型吗?

标签: php laravel


【解决方案1】:

//快速修复:

//假设您的帖子表称为帖子,并且在您的 blogetccmets 表中 //您有一个指向原始帖子的 post_id 列。尝试类似

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

在您的刀片模板中,访问每个帖子的 cmets 计数,如下所示..

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

...
@endforeach

【讨论】:

    【解决方案2】:

    您可以使用withCount() 获取特定帖子的 cmets 计数:

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

    上述要求您的comments 模型具有comments 关系:

    Post模特:

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

    BlogEtcComment模特:

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

    然后在刀片中:

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

    【讨论】:

      猜你喜欢
      • 2018-03-20
      • 2017-07-29
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2012-08-22
      • 2015-09-20
      相关资源
      最近更新 更多