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

<i id='94uxk'><tr id='94uxk'><dt id='94uxk'><q id='94uxk'><span id='94uxk'><b id='94uxk'><form id='94uxk'><ins id='94uxk'></ins><ul id='94uxk'></ul><sub id='94uxk'></sub></form><legend id='94uxk'></legend><bdo id='94uxk'><pre id='94uxk'><center id='94uxk'></center></pre></bdo></b><th id='94uxk'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='94uxk'><tfoot id='94uxk'></tfoot><dl id='94uxk'><fieldset id='94uxk'></fieldset></dl></div>
<tfoot id='94uxk'></tfoot>

<small id='94uxk'></small><noframes id='94uxk'>

      <bdo id='94uxk'></bdo><ul id='94uxk'></ul>

        <legend id='94uxk'><style id='94uxk'><dir id='94uxk'><q id='94uxk'></q></dir></style></legend>

        Laravel 中關(guān)系的計數(shù)關(guān)系

        count relation of relation in laravel(Laravel 中關(guān)系的計數(shù)關(guān)系)
        <i id='4xCFU'><tr id='4xCFU'><dt id='4xCFU'><q id='4xCFU'><span id='4xCFU'><b id='4xCFU'><form id='4xCFU'><ins id='4xCFU'></ins><ul id='4xCFU'></ul><sub id='4xCFU'></sub></form><legend id='4xCFU'></legend><bdo id='4xCFU'><pre id='4xCFU'><center id='4xCFU'></center></pre></bdo></b><th id='4xCFU'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='4xCFU'><tfoot id='4xCFU'></tfoot><dl id='4xCFU'><fieldset id='4xCFU'></fieldset></dl></div>
          • <bdo id='4xCFU'></bdo><ul id='4xCFU'></ul>

              <tbody id='4xCFU'></tbody>

              <small id='4xCFU'></small><noframes id='4xCFU'>

              <legend id='4xCFU'><style id='4xCFU'><dir id='4xCFU'><q id='4xCFU'></q></dir></style></legend>
                <tfoot id='4xCFU'></tfoot>

                1. 本文介紹了Laravel 中關(guān)系的計數(shù)關(guān)系的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  假設(shè)我有一個像這樣的 Conversation 模型:

                  Suppose I have a Conversation model like this :

                  class Conversation extends Model
                  {
                      public function questions (){
                          return $this->hasMany('AppQuestion','conversation_id','conversation_id');
                      }
                      public function category ()
                      {
                          return $this->belongsTo('AppCategory', 'cat', 'cat_id');
                      }
                  
                  }
                  

                  還有一個像這樣的 Question 模型:

                  And a Question model like this:

                  class Question extends Model
                  {
                      public function conversation ()
                      {
                          return $this->belongsTo('AppConversation', 'conversation_id', 'conversation_id');
                      }
                  }
                  

                  如您所見,這兩者之間存在 hasMany 關(guān)系.

                  As you can see there is a hasMany relation between those two.

                  另一方面,有一個像下面這樣的 CategoryConversation 模型有關(guān)系:

                  In the other hand there is a Category like below that has a relation with Conversation model :

                  class Category extends Node
                  {
                      public function conversations (){
                          return $this->hasMany('AppConversation','cat','cat_id');
                      }
                  }
                  

                  現(xiàn)在我想將一個名為 question_count 的屬性附加到 Category 來計算每個類別的對話的所有問題.為此,我添加了這個:

                  Now I want to append an attribute named question_count to Category that counts all questions of conversations of each category. for that I added this :

                      public function getQuestionsCountAttribute ()
                      {
                          return $this->conversations->questions->count();
                      }
                  

                  但是在獲取類別時出現(xiàn)此錯誤:

                  But when fetch a category I got this error :

                  ErrorException in Category.php line 59:
                  Undefined property: IlluminateDatabaseEloquentCollection::$questions
                  

                  我做了什么?如何在最小服務(wù)器過載的情況下計算關(guān)系的關(guān)系?

                  What did I do? how can I count relations of a relation with minimum server overloading?

                  我使用的是 Laravel 5.3.4.

                  I am using laravel 5.3.4.

                  推薦答案

                  我認(rèn)為這里你需要一個有很多的關(guān)系.

                  I think that you need a has many through relationship here.

                  你做錯了什么:

                  當(dāng)你寫$this->conversations->questions時,這是行不通的,因為questions單個對話的關(guān)系 而不是對話的集合(這里,$this->conversations 是一個集合)

                  When you write $this->conversations->questions, this can't work, because the questions are a relation of a single conversation and not of a collection of conversations (here, $this->conversations is a Collection)

                  解決辦法:

                  使用 hasManyThrough 關(guān)系:

                  Using hasManyThrough relation:

                  您可以在在此頁面上找到有關(guān)此關(guān)系的文檔,如果我的解釋不好

                  You can find the documentation for this relation on this page, if my explanation is bad

                  基礎(chǔ)是,你需要在你的 Category 模型上定義一個關(guān)系:

                  The basics are, you need to define a relation on your Category model:

                  class Category extends Node
                  {
                      public function conversations ()
                      {
                          return $this->hasMany('AppConversation');
                      }
                  
                      public function questions ()
                      {
                          return $this->hasManyThrough('AppQuestion', 'AppConversation');
                      }
                  }
                  

                  (我會讓你查看非標(biāo)準(zhǔn)外鍵的文檔)

                  (I will let your look into the documentation for your non standards foreign keys)

                  然后你應(yīng)該可以使用:$category->questions->count()

                  這篇關(guān)于Laravel 中關(guān)系的計數(shù)關(guān)系的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準(zhǔn)備好的語句amp;foreach 循環(huán))
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務(wù)器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數(shù))
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結(jié)果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“l(fā)ocalhost的訪問被拒絕)

                    <bdo id='3eT9q'></bdo><ul id='3eT9q'></ul>
                  • <i id='3eT9q'><tr id='3eT9q'><dt id='3eT9q'><q id='3eT9q'><span id='3eT9q'><b id='3eT9q'><form id='3eT9q'><ins id='3eT9q'></ins><ul id='3eT9q'></ul><sub id='3eT9q'></sub></form><legend id='3eT9q'></legend><bdo id='3eT9q'><pre id='3eT9q'><center id='3eT9q'></center></pre></bdo></b><th id='3eT9q'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='3eT9q'><tfoot id='3eT9q'></tfoot><dl id='3eT9q'><fieldset id='3eT9q'></fieldset></dl></div>
                    <legend id='3eT9q'><style id='3eT9q'><dir id='3eT9q'><q id='3eT9q'></q></dir></style></legend>
                      1. <tfoot id='3eT9q'></tfoot>
                          <tbody id='3eT9q'></tbody>

                          <small id='3eT9q'></small><noframes id='3eT9q'>

                            主站蜘蛛池模板: 亚洲第一福利视频 | 亚洲第一色站 | 日韩在线免费看 | 黑人中文字幕一区二区三区 | 精品久久久久一区 | 国产精品激情 | 欧美一区免费 | 成人免费在线观看 | 欧美成人a∨高清免费观看 欧美日韩中 | 黄网站免费在线观看 | 国产一区在线看 | 日韩欧美在线视频播放 | 中文字幕不卡在线观看 | 国产精品一区在线观看 | 欧美成人a∨高清免费观看 欧美日韩中 | 浮生影院免费观看中文版 | 日韩精品一区二区三区免费观看 | 成人精品久久 | 视频一区在线播放 | 日韩在线视频一区 | 亚洲成人一级 | aaa在线观看 | 操操操操操 | 日韩av一区二区在线观看 | 最新日韩精品 | 91国产视频在线 | 日韩精品在线免费观看 | 日韩aⅴ视频 | 欧美九九九 | 国产精品久久久亚洲 | 日韩和的一区二区 | 一区二区三区在线 | 欧美福利久久 | 国产在线高清 | 日韩中文在线观看 | 国产精品久久久久久久久久久久久 | 精品久久久久久久久久久久久久久久久 | 久久精品视频12 | 国产福利在线 | 中文字幕在线第一页 | 91精品国产91久久久久久密臀 |