pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<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="thbttj5" 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 中關系的計數關系

        count relation of relation in laravel(Laravel 中關系的計數關系)
        <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="5tnv57t" 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 中關系的計數關系的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  假設我有一個像這樣的 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 關系.

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

                  另一方面,有一個像下面這樣的 CategoryConversation 模型有關系:

                  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');
                      }
                  }
                  

                  現在我想將一個名為 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();
                      }
                  

                  但是在獲取類別時出現此錯誤:

                  But when fetch a category I got this error :

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

                  我做了什么?如何在最小服務器過載的情況下計算關系的關系?

                  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.

                  推薦答案

                  我認為這里你需要一個有很多的關系.

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

                  你做錯了什么:

                  當你寫$this->conversations->questions時,這是行不通的,因為questions單個對話的關系 而不是對話的集合(這里,$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 關系:

                  Using hasManyThrough relation:

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

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

                  基礎是,你需要在你的 Category 模型上定義一個關系:

                  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');
                      }
                  }
                  

                  (我會讓你查看非標準外鍵的文檔)

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

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

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

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

                  相關文檔推薦

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

                    <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="bnr7b5b" 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'>

                            主站蜘蛛池模板: 深圳货架厂家_金丽声精品货架_广东金丽声展示设备有限公司官网 | 北京工业设计公司-产品外观设计-产品设计公司-千策良品工业设计 北京翻译公司-专业合同翻译-医学标书翻译收费标准-慕迪灵 | 直流电能表-充电桩电能表-导轨式电能表-智能电能表-浙江科为电气有限公司 | 空气能暖气片,暖气片厂家,山东暖气片,临沂暖气片-临沂永超暖通设备有限公司 | HEYL硬度计量泵-荧光法在线溶解氧仪-净时测控技术(上海)有限公司 | 偏心半球阀-电动偏心半球阀-调流调压阀-旋球阀-上欧阀门有限公司 | 双齿辊破碎机-大型狼牙破碎机视频-对辊破碎机价格/型号图片-金联机械设备生产厂家 | 钢衬四氟管道_钢衬四氟直管_聚四氟乙烯衬里管件_聚四氟乙烯衬里管道-沧州汇霖管道科技有限公司 | 皮带式输送机械|链板式输送机|不锈钢输送机|网带输送机械设备——青岛鸿儒机械有限公司 | 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 南京泽朗生物科技有限公司| 焦作网 WWW.JZRB.COM| 武汉宣传片制作-视频拍摄-企业宣传片公司-武汉红年影视 | 板材品牌-中国胶合板行业十大品牌-环保板材-上海声达板材 | 【星耀裂变】_企微SCRM_任务宝_视频号分销裂变_企业微信裂变增长_私域流量_裂变营销 | 焊管生产线_焊管机组_轧辊模具_焊管设备_焊管设备厂家_石家庄翔昱机械 | 营养师网,营养师考试时间,报名入口—网站首页 | 尾轮组_头轮组_矿用刮板_厢式刮板机_铸石刮板机厂家-双驰机械 | 板式换网器_柱式换网器_自动换网器-郑州海科熔体泵有限公司 | 扒渣机,铁水扒渣机,钢水扒渣机,铁水捞渣机,钢水捞渣机-烟台盛利达工程技术有限公司 | 杭州货架订做_组合货架公司_货位式货架_贯通式_重型仓储_工厂货架_货架销售厂家_杭州永诚货架有限公司 | 注浆压力变送器-高温熔体传感器-矿用压力传感器|ZHYQ朝辉 | 南京技嘉环保科技有限公司-杀菌除臭剂|污水|垃圾|厕所|橡胶厂|化工厂|铸造厂除臭剂 | POS机官网 - 拉卡拉POS机免费办理|官网在线申请入口 | 搪瓷搅拌器,搪玻璃搅拌器,搪玻璃冷凝器_厂家-淄博越宏化工设备 | 臻知网大型互动问答社区-你的问题将在这里得到解答!-无锡据风网络科技有限公司 | 粘弹体防腐胶带,聚丙烯防腐胶带-全民塑胶 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 政府回应:200块在义乌小巷能买到爱情吗?——揭秘打工族省钱约会的生存智慧 | 上海防爆真空干燥箱-上海防爆冷库-上海防爆冷柜?-上海浦下防爆设备厂家? | NMRV减速机|铝合金减速机|蜗轮蜗杆减速机|NMRV减速机厂家-东莞市台机减速机有限公司 | 上海防爆真空干燥箱-上海防爆冷库-上海防爆冷柜?-上海浦下防爆设备厂家? | 泥浆在线密度计厂家-防爆数字压力表-膜盒-远传压力表厂家-江苏大亚自控设备有限公司 | 水质传感器_水质监测站_雨量监测站_水文监测站-山东水境传感科技有限公司 | 在线悬浮物浓度计-多参数水质在线检测仪-上海沃懋仪表科技有限公司 | 合肥钣金加工-安徽激光切割加工-机箱机柜加工厂家-合肥通快 | 铣床|万能铣床|立式铣床|数控铣床|山东滕州万友机床有限公司 | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | 潍坊大集网-潍坊信息港-潍坊信息网 | 土壤有机碳消解器-石油|表层油类分析采水器-青岛溯源环保设备有限公司 |