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

    <legend id='4B1mN'><style id='4B1mN'><dir id='4B1mN'><q id='4B1mN'></q></dir></style></legend>

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

    1. <tfoot id='4B1mN'></tfoot>

    2. <i id='4B1mN'><tr id='4B1mN'><dt id='4B1mN'><q id='4B1mN'><span id='4B1mN'><b id='4B1mN'><form id='4B1mN'><ins id='4B1mN'></ins><ul id='4B1mN'></ul><sub id='4B1mN'></sub></form><legend id='4B1mN'></legend><bdo id='4B1mN'><pre id='4B1mN'><center id='4B1mN'></center></pre></bdo></b><th id='4B1mN'></th></span></q></dt></tr></i><div class="22cmrho" id='4B1mN'><tfoot id='4B1mN'></tfoot><dl id='4B1mN'><fieldset id='4B1mN'></fieldset></dl></div>
      • <bdo id='4B1mN'></bdo><ul id='4B1mN'></ul>
    3. Laravel 多對多加載相關(guān)模型計(jì)數(shù)

      Laravel many to many loading related models with count(Laravel 多對多加載相關(guān)模型計(jì)數(shù))

    4. <legend id='pnSZO'><style id='pnSZO'><dir id='pnSZO'><q id='pnSZO'></q></dir></style></legend>

        <small id='pnSZO'></small><noframes id='pnSZO'>

          • <i id='pnSZO'><tr id='pnSZO'><dt id='pnSZO'><q id='pnSZO'><span id='pnSZO'><b id='pnSZO'><form id='pnSZO'><ins id='pnSZO'></ins><ul id='pnSZO'></ul><sub id='pnSZO'></sub></form><legend id='pnSZO'></legend><bdo id='pnSZO'><pre id='pnSZO'><center id='pnSZO'></center></pre></bdo></b><th id='pnSZO'></th></span></q></dt></tr></i><div class="ht8hazw" id='pnSZO'><tfoot id='pnSZO'></tfoot><dl id='pnSZO'><fieldset id='pnSZO'></fieldset></dl></div>
              <tbody id='pnSZO'></tbody>
            <tfoot id='pnSZO'></tfoot>
              <bdo id='pnSZO'></bdo><ul id='pnSZO'></ul>
                本文介紹了Laravel 多對多加載相關(guān)模型計(jì)數(shù)的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我正在嘗試鏈接 4 個(gè)表,并添加一個(gè)自定義字段,該字段通過使用 laravel 計(jì)算一些相關(guān)表的 id 來計(jì)算.我在 SQL 中有這個(gè)可以做我想要的,但我認(rèn)為它可以提高效率:

                I am trying to link 4 tables and also add a custom field calculated by counting the ids of some related tables using laravel. I have this in SQL which does what I want, but I think it can be made more efficient:

                DB::select('SELECT 
                                        posts.*,
                                          users.id AS users_id, users.email,users.username,
                                          GROUP_CONCAT(tags.tag ORDER BY posts_tags.id) AS tags,
                                          COUNT(DISTINCT comments.id) AS NumComments, 
                                          COUNT(DISTINCT vote.id) AS NumVotes
                                        FROM 
                                          posts    
                                          LEFT JOIN comments ON comments.posts_id = posts.id
                                          LEFT JOIN users ON users.id = posts.author_id
                                          LEFT JOIN vote  ON vote.posts_id = posts.id
                                          LEFT JOIN posts_tags  ON posts_tags.posts_id = posts.id
                                          LEFT JOIN tags  ON tags.id = posts_tags.tags_id
                
                                        GROUP BY 
                                          posts.id, 
                                          posts.post_title');
                

                我嘗試通過這樣做使用 eloquent 來實(shí)現(xiàn)它:

                I tried to implement it using eloquent by doing this:

                $trending=Posts::with(array('comments' => function($query)
                                {
                                    $query->select(DB::raw('COUNT(DISTINCT comments.id) AS NumComments'));
                
                                },'user','vote','tags'))->get();
                

                但是 NumComments 值沒有顯示在查詢結(jié)果中.任何線索如何去做?

                However the NumComments value is not showing up in the query results. Any clue how else to go about it?

                推薦答案

                使用 with 不能這樣做,因?yàn)樗鼒?zhí)行單獨(dú)的查詢.

                You can't do that using with, because it executes separate query.

                您需要的是簡單的join.只需將您的查詢翻譯成類似的內(nèi)容:

                What you need is simple join. Just translate the query you have to something like:

                Posts::join('comments as c', 'posts.id', '=', 'c.id')
                    ->selectRaw('posts.*, count(distinct c.id) as numComments')
                    ->groupBy('posts.id', 'posts.post_title')
                    ->with('user', 'vote', 'tags')
                    ->get();
                

                那么集合中的每個(gè)帖子都會有計(jì)數(shù)屬性:

                then each post in the collection will have count attribute:

                $post->numComments;
                

                <小時(shí)>

                但是,您可以通過以下關(guān)系更輕松:

                雖然第一個(gè)解決方案在性能方面更好(除非您有大數(shù)據(jù),否則可能不會引起注意)


                However you can make it easier with relations like below:

                Though first solution is better in terms of performance (might not be noticeable unless you have big data)

                // helper relation
                public function commentsCount()
                {
                    return $this->hasOne('Comment')->selectRaw('posts_id, count(*) as aggregate')->groupBy('posts_id');
                }
                
                // accessor for convenience
                public function getCommentsCountAttribute()
                {
                    // if relation not loaded already, let's load it now
                    if ( ! array_key_exists('commentsCount', $this->relations)) $this->load('commentsCount');
                
                    return $this->getRelation('commentsCount')->aggregate;
                }
                

                這將允許您執(zhí)行此操作:

                This will allow you to do this:

                $posts = Posts::with('commentsCount', 'tags', ....)->get();
                // then each post:
                $post->commentsCount;
                

                對于多對多關(guān)系:

                public function tagsCount()
                {
                    return $this->belongsToMany('Tag')->selectRaw('count(tags.id) as aggregate')->groupBy('pivot_posts_id');
                }
                
                public function getTagsCountAttribute()
                {
                    if ( ! array_key_exists('tagsCount', $this->relations)) $this->load('tagsCount');
                
                    $related = $this->getRelation('tagsCount')->first();
                
                    return ($related) ? $related->aggregate : 0;
                }
                

                可以在此處找到更多此類示例 http://softonsofa.com/tweaking-eloquent-relations-how-to-get-hasmany-relation-count-efficiently/

                More examples like this can be found here http://softonsofa.com/tweaking-eloquent-relations-how-to-get-hasmany-relation-count-efficiently/

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

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

                相關(guān)文檔推薦

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

                  <tfoot id='mNuLM'></tfoot>

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

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

                          <legend id='mNuLM'><style id='mNuLM'><dir id='mNuLM'><q id='mNuLM'></q></dir></style></legend>
                        • 主站蜘蛛池模板: 奥因-光触媒除甲醛公司-除甲醛加盟公司十大品牌 | 南京泽朗生物科技有限公司-液体饮料代加工_果汁饮料代加工_固体饮料代加工 | 混合反应量热仪-高温高压量热仪-微机差热分析仪DTA|凯璞百科 | 液压压力机,液压折弯机,液压剪板机,模锻液压机-鲁南新力机床有限公司 | 工业胀紧套_万向节联轴器_链条-规格齐全-型号选购-非标订做-厂家批发价格-上海乙谛精密机械有限公司 | 岩石钻裂机-液压凿岩机-劈裂机-挖改钻_湖南烈岩科技有限公司 | 长沙中央空调维修,中央空调清洗维保,空气能热水工程,价格,公司就找维小保-湖南维小保环保科技有限公司 | 气弹簧定制-气动杆-可控气弹簧-不锈钢阻尼器-工业气弹簧-可调节气弹簧厂家-常州巨腾气弹簧供应商 | 纸张环压仪-纸张平滑度仪-杭州纸邦自动化技术有限公司 | POS机办理_个人POS机免费领取 - 银联POS机申请首页 | 纯水设备_苏州皙全超纯水设备水处理设备生产厂家 | 中天寰创-内蒙古钢结构厂家|门式刚架|钢结构桁架|钢结构框架|包头钢结构煤棚 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 我车网|我关心的汽车资讯_汽车图片_汽车生活! | 山东臭氧发生器,臭氧发生器厂家-山东瑞华环保设备 | 分光色差仪,测色仪,反透射灯箱,爱色丽分光光度仪,美能达色差仪维修_苏州欣美和仪器有限公司 | 日本SMC气缸接头-速度控制阀-日本三菱伺服电机-苏州禾力自动化科技有限公司 | 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | 全自动在线分板机_铣刀式在线分板机_曲线分板机_PCB分板机-东莞市亿协自动化设备有限公司 | 合肥防火门窗/隔断_合肥防火卷帘门厂家_安徽耐火窗_良万消防设备有限公司 | 陕西自考报名_陕西自学考试网 | 塑胶跑道_学校塑胶跑道_塑胶球场_运动场材料厂家_中国塑胶跑道十大生产厂家_混合型塑胶跑道_透气型塑胶跑道-广东绿晨体育设施有限公司 | 超声波焊接机,振动摩擦焊接机,激光塑料焊接机,超声波焊接模具工装-德召尼克(常州)焊接科技有限公司 | 硬质合金模具_硬质合金非标定制_硬面加工「生产厂家」-西迪技术股份有限公司 | 哈希余氯测定仪,分光光度计,ph在线监测仪,浊度测定仪,试剂-上海京灿精密机械有限公司 | 砂磨机_立式纳米砂磨机_实验室砂磨机-广州儒佳化工设备厂家 | 工业机械三维动画制作 环保设备原理三维演示动画 自动化装配产线三维动画制作公司-南京燃动数字 聚合氯化铝_喷雾聚氯化铝_聚合氯化铝铁厂家_郑州亿升化工有限公司 | 酸度计_PH计_特斯拉计-西安云仪 纯水电导率测定仪-万用气体检测仪-低钠测定仪-米沃奇科技(北京)有限公司www.milwaukeeinst.cn | 建筑资质代办-建筑企业资质代办机构-建筑资质代办公司 | 蓄电池回收,ups电池后备电源回收,铅酸蓄电池回收,机房电源回收-广州益夫铅酸电池回收公司 | 伸缩器_伸缩接头_传力接头-巩义市润达管道设备制造有限公司 | 专业甜品培训学校_广东糖水培训_奶茶培训_特色小吃培训_广州烘趣甜品培训机构 | 棕刚玉-白刚玉厂家价格_巩义市东翔净水材料厂 | 爆炸冲击传感器-无线遥测传感器-航天星百科 | 股指期货-期货开户-交易手续费佣金加1分-保证金低-期货公司排名靠前-万利信息开户 | 药品冷藏箱厂家_低温冰箱_洁净工作台-济南欧莱博电子商务有限公司官网 | 山东彩钢板房,山东彩钢活动房,临沂彩钢房-临沂市贵通钢结构工程有限公司 | 体视显微镜_荧光生物显微镜_显微镜报价-微仪光电生命科学显微镜有限公司 | 阿尔法-MDR2000无转子硫化仪-STM566 SATRA拉力试验机-青岛阿尔法仪器有限公司 | 小小作文网_中小学优秀作文范文大全 | 杰恒蠕动泵-蠕动泵专业厂家-19年专注蠕动泵 |