<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                >[danger] withCount() 關聯數量統計 應用場景: 不需要獲取關聯數據,只需要關聯數據的統計 >[danger] 使用示例 ![](https://img.kancloud.cn/aa/c5/aac573db257b2bfeb5568cdc7d465577_415x251.png) ```php <?php namespace app\model; /** * 文章模型 */ class Article extends \think\Model { public function test() { // self::withCount('關聯方法名')->select(); // self::withCount(['關聯方法名' => '自定義屬性名'])->select(); $data = self::withCount(['comments' => 'comm_count'])->select(); // 查看數據 dump($data->toArray()); } /** * 一對多關聯 文章關聯多個評論 */ public function comments() { return $this->hasMany(Comments::class, 'aid'); } } ``` >[danger] `關聯預載入`和`關聯統計`同時使用 ![](https://img.kancloud.cn/a6/8b/a68b7c1488b92a1e3a54a09879acb303_663x573.png) ~~~ $data = self::with('comments') ->withCount(['comments' => 'comm_count']) ->select(); ~~~ >[danger] 關聯統計屬性名 關聯統計會自動添加一個以`關聯方法名` + `_count` 為名稱的屬性保存到模型數據對象中 >[danger] 查詢多個統計字段 ~~~ // 統計一個關聯數據 $data = self::withCount('comments')->select(); // 統計多個關聯數據 $data = self::withCount(['comments', 'likes'])->select(); ~~~ >[danger] 使用閉包的方式對關聯統計進行條件過濾 ~~~ $data = self::withCount(['comments' => function($query) { // comments評論表id大于3 $query->where('id', '>', 3); // 沒有使用閉包時 自定義關聯統計屬性名 // withCount(['關聯方法' => '自定義屬性名']) // 官方手冊: 使用了閉包方式 自定義屬性名的方法如下 // 但是測試結果 定義屬性名無效 $alias = 'comm_count'; }, 'likes'])->select(); ~~~ >[danger] 所有的關聯統計方法都可以多次調用 ~~~ $data = self::withCount('comments') ->withCount('likes') ->select(); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看