久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

        • <bdo id='fCTm0'></bdo><ul id='fCTm0'></ul>
      1. <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 class="qwawimqqmiuu" 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'>

      2. Laravel hasMany 關系統計帖子的點贊數和評論數

        Laravel hasMany relation count number of likes and comments on post(Laravel hasMany 關系統計帖子的點贊數和評論數)
        <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 class="qwawimqqmiuu" id='HLffE'><tfoot id='HLffE'></tfoot><dl id='HLffE'><fieldset id='HLffE'></fieldset></dl></div>
          • <bdo id='HLffE'></bdo><ul id='HLffE'></ul>
              <tbody id='HLffE'></tbody>
            <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 關系統計帖子的點贊數和評論數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  代碼:

                  $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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)

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

                        <tbody id='rDNT1'></tbody>

                      <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 class="qwawimqqmiuu" 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>
                            主站蜘蛛池模板: 久久1区 | 欧美亚洲国产日韩 | 成人午夜视频在线观看 | 中文字幕一区二区三区乱码在线 | 日本三级全黄三级a | 亚洲成av| 亚洲国产一区在线 | 超碰伊人| 欧美日韩综合精品 | 久久综合欧美 | av中文字幕在线观看 | 国产在线色| 国产精品资源在线 | 九九热精品在线视频 | 国产欧美在线视频 | 亚洲精选一区 | av手机在线免费观看 | 精品国产乱码久久久久久闺蜜 | 亚洲手机视频在线 | 久久av.com | 日日操网站 | 国产免费让你躁在线视频 | 精产国产伦理一二三区 | 亚洲国产精选 | 一区二区三区中文字幕 | 成年女人免费v片 | 人人干在线视频 | 亚洲综合在线播放 | 九色网址| 99成人免费视频 | 国产一级视屏 | 91久久夜色精品国产网站 | 欧美一区二区三区视频在线播放 | 国产精品成人一区二区 | 欧美日韩a | 日韩欧美网 | 国产精品18久久久 | 蜜桃视频在线观看免费视频网站www | 九九热这里只有精品6 | 免费一区二区 | 日韩成人在线观看 |