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

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

  • <legend id='DirmZ'><style id='DirmZ'><dir id='DirmZ'><q id='DirmZ'></q></dir></style></legend>

  • <small id='DirmZ'></small><noframes id='DirmZ'>

        <tfoot id='DirmZ'></tfoot>

        laravel 4 雄辯的急切加載關系計數

        laravel 4 eloquent eager load relation count(laravel 4 雄辯的急切加載關系計數)
        <i id='Jl2oy'><tr id='Jl2oy'><dt id='Jl2oy'><q id='Jl2oy'><span id='Jl2oy'><b id='Jl2oy'><form id='Jl2oy'><ins id='Jl2oy'></ins><ul id='Jl2oy'></ul><sub id='Jl2oy'></sub></form><legend id='Jl2oy'></legend><bdo id='Jl2oy'><pre id='Jl2oy'><center id='Jl2oy'></center></pre></bdo></b><th id='Jl2oy'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Jl2oy'><tfoot id='Jl2oy'></tfoot><dl id='Jl2oy'><fieldset id='Jl2oy'></fieldset></dl></div>

            1. <legend id='Jl2oy'><style id='Jl2oy'><dir id='Jl2oy'><q id='Jl2oy'></q></dir></style></legend>

                  <tbody id='Jl2oy'></tbody>
                <tfoot id='Jl2oy'></tfoot>
              1. <small id='Jl2oy'></small><noframes id='Jl2oy'>

                • <bdo id='Jl2oy'></bdo><ul id='Jl2oy'></ul>
                  本文介紹了laravel 4 雄辯的急切加載關系計數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有一個具有多個定義關系的復雜模型.在這個例子中,我想計算 Like 模型并創建一個名為 likes 的屬性,以便它可以從 REST 服務返回.

                  I have a complex Model with multiple defined relations. In this example I would want to count the Like model and create a property named likes so it can be returned from a REST service.

                  是否可以將模型計數預先加載到動態屬性中?

                  Is it possible to eager load a model count into a dynamic property?

                  $beat = Post::with(
                           array(
                              'user',
                              'likes' => function($q){
                                  $q->count();
                              }
                          ))
                          ->where('id', $id)
                          ->first();
                  

                  推薦答案

                  假設您有 Post->hasMany->Like 關系,并且您已聲明 Likes 關系為:

                  Assuming you are having Post->hasMany->Like relationship and you have declared likes relationship as:

                  class Post{
                  
                    public function likes(){
                     return $this->hasMany('Like');
                    }
                  }
                  

                  創建一個新函數,比如 likeCountRelation 為:

                  create a new function say likeCountRelation as:

                  public function likeCountRelation()
                  {
                      $a = $this->likes();
                  
                      return $a->selectRaw($a->getForeignKey() . ', count(*) as count')->groupBy($a->getForeignKey());
                  }
                  

                  現在您可以將 __get() 函數重寫為:

                  now you can override __get() function as:

                  public function __get($attribute)
                  {
                  
                      if (array_key_exists($attribute, $this->attributes)) {
                          return $this->attributes[$attribute];
                      }
                  
                      switch ($attribute) {
                  
                          case 'likesCount':
                              return $this->attributes[$attribute] = $this->likesCountRelation->first() ? $this->likesCountRelation->first()->count : 0;
                              break;
                  
                          default:
                              return parent::__get($attribute);
                  
                      }
                  }
                  

                  或者你可以使用 getattribute 函數:

                  or you can use getattribute function as :

                  public function getLikesCountAttribute(){
                   return $this->likesCountRelation->first() ? $this->likesCountRelation->first()->count : 0;
                  }
                  

                  并且只需將 likesCount 作為 $post->likesCount 訪問,您甚至可以像這樣急切加載它:

                  and simply access likesCount as $post->likesCount you can even eager load it like:

                  $posts=Post::with('likesCountRelation')->get();
                   foreach($post as $post){
                    $post->likesCount;
                   }
                  

                  注意:相同的邏輯可用于變形許多關系.

                  NOTE: Same logic can be used for morph many relationships.

                  這篇關于laravel 4 雄辯的急切加載關系計數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 找不到驅動程序)

                  1. <legend id='7SMwi'><style id='7SMwi'><dir id='7SMwi'><q id='7SMwi'></q></dir></style></legend>

                    <tfoot id='7SMwi'></tfoot>
                      <bdo id='7SMwi'></bdo><ul id='7SMwi'></ul>

                            <small id='7SMwi'></small><noframes id='7SMwi'>

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

                            主站蜘蛛池模板: 日韩欧美三区 | 日韩三级免费网站 | 韩日三级| 91精品一区二区 | h漫在线观看 | 国产日韩欧美一区二区 | 国产在线一区二区三区 | 日日夜精品视频 | 日韩中文字幕 | 性高朝久久久久久久3小时 av一区二区三区四区 | 国产精品视频一二三区 | 精品福利在线 | 欧美一区二区在线播放 | 精品久久影院 | 天天久久 | 婷婷久久综合 | 欧美一区二区成人 | 国产精品视频 | 国产成人综合在线 | 久久国| 羞羞视频在线观看 | 可以免费看的毛片 | 亚洲一区二区视频在线观看 | 亚洲一区二区在线免费观看 | 欧美性受xxxx | 在线小视频| 亚洲国产精品一区二区第一页 | 久久蜜桃av一区二区天堂 | 免费国产一区 | 欧美一区二区三区在线播放 | 国产精久久久久久 | 国产精品99久久久久久久久久久久 | www.久久精品 | 国产伦精品一区二区 | 国产在线高清 | 欧美精品一区二区三区四区五区 | 午夜影院视频在线观看 | 国产91精品久久久久久久网曝门 | 亚洲综合色婷婷 | 色视频www在线播放国产人成 | 亚洲国产aⅴ成人精品无吗 欧美激情欧美激情在线五月 |