<i id='HLffE'><tr id='HLffE'><dt id='HLffE'><q id='HLffE'><span id='HLffE'><b id='HLffE'><form id='HLffE'><ins id='HLffE'></ins><ul id='HLffE'></ul><sub id='HLffE'></sub></form><legend id='HLffE'></legend><bdo id='HLffE'><pre id='HLffE'><center id='HLffE'></center></pre></bdo></b><th id='HLffE'></th></span></q></dt></tr></i><div id='HLffE'><tfoot id='HLffE'></tfoot><dl id='HLffE'><fieldset id='HLffE'></fieldset></dl></div>
    • <bdo id='HLffE'></bdo><ul id='HLffE'></ul>
    <legend id='HLffE'><style id='HLffE'><dir id='HLffE'><q id='HLffE'></q></dir></style></legend>

  • <tfoot id='HLffE'></tfoot>

        <small id='HLffE'></small><noframes id='HLffE'>

        Laravel hasMany 关系统计帖子的点赞数和评论数

        时间:2023-09-24
          <tbody id='fCTm0'></tbody>

        <tfoot id='fCTm0'></tfoot>

            • <bdo id='fCTm0'></bdo><ul id='fCTm0'></ul>
              <i id='fCTm0'><tr id='fCTm0'><dt id='fCTm0'><q id='fCTm0'><span id='fCTm0'><b id='fCTm0'><form id='fCTm0'><ins id='fCTm0'></ins><ul id='fCTm0'></ul><sub id='fCTm0'></sub></form><legend id='fCTm0'></legend><bdo id='fCTm0'><pre id='fCTm0'><center id='fCTm0'></center></pre></bdo></b><th id='fCTm0'></th></span></q></dt></tr></i><div id='fCTm0'><tfoot id='fCTm0'></tfoot><dl id='fCTm0'><fieldset id='fCTm0'></fieldset></dl></div>

              <legend id='fCTm0'><style id='fCTm0'><dir id='fCTm0'><q id='fCTm0'></q></dir></style></legend>

                <small id='fCTm0'></small><noframes id='fCTm0'>

                • 本文介绍了Laravel hasMany 关系统计帖子的点赞数和评论数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  代码:

                  $posts = Jumpsite::find($jid)
                              ->posts()
                              ->with('comments')
                              ->with('likes')
                              ->with('number_of_comments')
                              ->with('number_of_likes')
                              ->where('reply_to', 0)
                              ->orderBy('pid', 'DESC')
                              ->paginate(10);
                  

                  每个帖子都有评论和喜欢.我最初只显示一些评论以避免大量加载.但我想显示每个帖子的总评论数和点赞数.我该怎么做?

                  Each post has a comment and likes. I only display a few of the comments initially to avoid large loads. But I want to show how many the total comments and likes for each post. How do I do this?

                  型号代码:

                  public function likes()
                  {
                      return $this->hasMany('Like', 'pid', 'pid');
                  }
                  
                  public function comments()
                  {
                      return $this->hasMany('Post', 'reply_to', 'pid')->with('likes')->take(4);
                  }
                  
                  public function number_of_likes()
                  {
                      return $this->hasMany('Like', 'pid', 'pid')->count();
                  }
                  

                  注意:

                  This is an API. All will be returned as JSON.
                  

                  <小时>

                  更新

                  回归

                  Post
                      author_id
                      message
                      Comments(recent 4)
                          user_id
                          message
                          post_date
                          Number_of_likes
                      Likes
                          user_id
                      Number_of_total_comments
                      Number_of_total_likes
                  

                  <小时>

                  更新

                  我如何返回数据

                  $posts  = $posts->toArray();
                  $posts  = $posts['data'];
                  
                  return Response::json(array(
                     'data' => $posts
                  ));
                  

                  只要使用它,我就可以在 json 中获取我想要的所有数据.但我也想加上总数.

                  Just by using that I get all the data i want in the json. But I also want to add the total counts.

                  更新

                  protected $appends = array('total_likes');
                  
                  public function getTotalLikesAttribute()
                  {
                     return $this->hasMany('Like')->whereUserId($this->uid)->wherePostId($this->pid)->count();
                  
                  }
                  

                  但得到错误:

                   Unknown column 'likes.post_id'
                  

                  <小时>

                  错误

                  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'likes.post_id' in 'where clause' (SQL: select count(*) as aggregate from `likes` where `likes`.`deleted_at` is null and `likes`.`post_id` = 4 and `pid` = 4 and `uid` = 1)
                  

                  推荐答案

                  在您的模型中放置以下访问器:

                  In your model place the following accessors:

                  点赞总数:

                   public function getTotalLikesAttribute()
                   {
                      return $this->hasMany('Like')->whereUserId($this->author_id)->count();
                  
                   }
                  

                  统计评论总数:

                  从你的描述中,我可以看到,你已经检索了作为评论的帖子数量

                  From your description, i can see, you have retrieving the number of posts as comments

                  public function getTotalCommentsAttribute()
                  {
                      return $this->hasMany('Post')->whereUserId($this->author_id)->count();    
                  }
                  

                  现在,从您的控制器:

                  $post  = Jumpsite::find($jid);
                  
                  // total comments
                  var_dump( $post->total_comments );
                  
                  // total Likes
                  var_dump( $post->total_likes );
                  

                  这篇关于Laravel hasMany 关系统计帖子的点赞数和评论数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHPStorm 无法识别 Laravel 5.0 中我的模型类的方法 下一篇:如何从 Laravel 中的数据库重新加载/刷新模型?

                  相关文章

                  最新文章

                  • <bdo id='rDNT1'></bdo><ul id='rDNT1'></ul>

                  <small id='rDNT1'></small><noframes id='rDNT1'>

                • <tfoot id='rDNT1'></tfoot>
                      <i id='rDNT1'><tr id='rDNT1'><dt id='rDNT1'><q id='rDNT1'><span id='rDNT1'><b id='rDNT1'><form id='rDNT1'><ins id='rDNT1'></ins><ul id='rDNT1'></ul><sub id='rDNT1'></sub></form><legend id='rDNT1'></legend><bdo id='rDNT1'><pre id='rDNT1'><center id='rDNT1'></center></pre></bdo></b><th id='rDNT1'></th></span></q></dt></tr></i><div id='rDNT1'><tfoot id='rDNT1'></tfoot><dl id='rDNT1'><fieldset id='rDNT1'></fieldset></dl></div>
                      <legend id='rDNT1'><style id='rDNT1'><dir id='rDNT1'><q id='rDNT1'></q></dir></style></legend>