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

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

        <bdo id='AoieR'></bdo><ul id='AoieR'></ul>
    1. <legend id='AoieR'><style id='AoieR'><dir id='AoieR'><q id='AoieR'></q></dir></style></legend>

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

    2. <tfoot id='AoieR'></tfoot>

      Laravel 急切加載限制

      Laravel eager loading with limit(Laravel 急切加載限制)
      • <bdo id='aRPAV'></bdo><ul id='aRPAV'></ul>
          <tbody id='aRPAV'></tbody>
        1. <i id='aRPAV'><tr id='aRPAV'><dt id='aRPAV'><q id='aRPAV'><span id='aRPAV'><b id='aRPAV'><form id='aRPAV'><ins id='aRPAV'></ins><ul id='aRPAV'></ul><sub id='aRPAV'></sub></form><legend id='aRPAV'></legend><bdo id='aRPAV'><pre id='aRPAV'><center id='aRPAV'></center></pre></bdo></b><th id='aRPAV'></th></span></q></dt></tr></i><div class="qtsvpv3" id='aRPAV'><tfoot id='aRPAV'></tfoot><dl id='aRPAV'><fieldset id='aRPAV'></fieldset></dl></div>
            <legend id='aRPAV'><style id='aRPAV'><dir id='aRPAV'><q id='aRPAV'></q></dir></style></legend>
          • <small id='aRPAV'></small><noframes id='aRPAV'>

              <tfoot id='aRPAV'></tfoot>

                本文介紹了Laravel 急切加載限制的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                問(wèn)題描述

                我有兩個(gè)表,比如users"和users_actions",其中users_actions"與用戶有 hasMany 關(guān)系:

                I have two tables, say "users" and "users_actions", where "users_actions" has an hasMany relation with users:

                用戶

                id | name | surname | email...
                

                操作

                id | id_action | id_user | log | created_at
                

                模型用戶.php

                class Users {
                    public function action()
                    { 
                       return $this->hasMany('Action', 'user_id')->orderBy('created_at', 'desc');
                    }
                }
                

                現(xiàn)在,我想檢索所有用戶列表以及他們的最后操作.

                Now, I want to retrieve a list of all users with their LAST action.

                我看到在做 Users::with('action')->get();通過(guò)僅獲取關(guān)系的第一個(gè)結(jié)果,可以輕松地給我最后一個(gè)操作:

                I saw that doing Users::with('action')->get(); can easily give me the last action by simply fetching only the first result of the relation:

                foreach ($users as $user) {
                   echo $user->action[0]->description;
                }
                

                但我當(dāng)然想避免這種情況,只為每個(gè)用戶選擇最后一個(gè)操作.

                but I wanted to avoid this of course, and just pick ONLY THE LAST action for EACH user.

                我嘗試使用約束,例如

                Users::with(['action' => function ($query) {
                    $query->orderBy('created_at', 'desc')
                          ->limit(1);
                    }])
                ->get();
                

                但是這給了我一個(gè)錯(cuò)誤的結(jié)果,因?yàn)?Laravel 執(zhí)行這個(gè)查詢:

                but that gives me an incorrect result since Laravel executes this query:

                SELECT * FROM users_actions WHERE user_id IN (1,2,3,4,5)
                ORDER BY created_at
                LIMIT 1
                

                這當(dāng)然是錯(cuò)誤的.是否有可能在不使用 Eloquent 對(duì)每條記錄執(zhí)行查詢的情況下獲得此信息?我是否犯了一些我沒(méi)有看到的明顯錯(cuò)誤?我剛開(kāi)始使用 Eloquent,有時(shí)關(guān)系會(huì)困擾我.

                which is of course wrong. Is there any possibility to get this without executing a query for each record using Eloquent? Am I making some obvious mistake I'm not seeing? I'm quite new to using Eloquent and sometimes relationship troubles me.

                代表目的的一部分,我還需要此功能來(lái)在關(guān)系中搜索,例如我想搜索 LAST ACTION = 'something' 的用戶

                A part from the representational purpose, I also need this feature for searching inside a relation, say for example I want to search users where LAST ACTION = 'something'

                我嘗試使用

                $actions->whereHas('action', function($query) {
                    $query->where('id_action', 1);
                });
                

                但是這給了我所有有一個(gè) action = 1 的用戶,因?yàn)樗且粋€(gè)日志,每個(gè)人都通過(guò)了這一步.


                but this gives me ALL the users which had had an action = 1, and since it's a log everyone passed that step.


                感謝@berkayk 看起來(lái)我解決了問(wèn)題的第一部分,但我仍然無(wú)法在關(guān)系中進(jìn)行搜索.

                Thanks to @berkayk looks like I solved the first part of my problem, but still I can't search within the relation.

                Actions::whereHas('latestAction', function($query) {
                    $query->where('id_action', 1);
                });
                

                仍然沒(méi)有執(zhí)行正確的查詢,它會(huì)生成如下內(nèi)容:

                still doesn't perform the right query, it generates something like:

                select * from `users` where 
                 (select count(*) 
                   from `users_action` 
                   where `users_action`.`user_id` = `users`.`id` 
                   and `id_action` in ('1')
                  ) >= 1 
                order by `created_at` desc
                

                我需要獲取 latest 操作為 1 的記錄

                I need to get the record where the latest action is 1

                推薦答案

                如果您想輕松獲得最新的 hasMany 相關(guān)模型,我由 @berbayk 鏈接的解決方案很酷.

                My solution linked by @berbayk is cool if you want to easily get latest hasMany related model.

                但是,它無(wú)法解決您所要求的其他部分,因?yàn)槭褂?where 子句查詢這種關(guān)系會(huì)導(dǎo)致您已經(jīng)經(jīng)歷過(guò)的幾乎相同的結(jié)果 - 所有行都會(huì)被返回,只有 latest 實(shí)際上不會(huì)是最新的(但最新的匹配 where 約束).

                However, it couldn't solve the other part of what you're asking for, since querying this relation with where clause would result in pretty much the same what you already experienced - all rows would be returned, only latest wouldn't be latest in fact (but latest matching the where constraint).

                給你:

                簡(jiǎn)單方法 - 獲取所有并過(guò)濾集合:

                User::has('actions')->with('latestAction')->get()->filter(function ($user) {
                   return $user->latestAction->id_action == 1;
                });
                

                <小時(shí)>

                或困難的方式 - 在 sql 中進(jìn)行(假設(shè)為 MySQL):


                or the hard way - do it in sql (assuming MySQL):

                User::whereHas('actions', function ($q) { 
                
                  // where id = (..subquery..)
                  $q->where('id', function ($q) { 
                
                    $q->from('actions as sub')
                      ->selectRaw('max(id)')
                      ->whereRaw('actions.user_id = sub.user_id');
                
                  })->where('id_action', 1);
                
                })->with('latestAction')->get();
                

                通過(guò)比較性能選擇其中一個(gè)解決方案 - 第一個(gè)將返回所有行并過(guò)濾可能的大集合.

                Choose one of these solutions by comparing performance - the first will return all rows and filter possibly big collection.

                后者將使用嵌套子查詢 (where('id', function () {..}) 運(yùn)行子查詢 (whereHas),所以兩種方式在大桌子上都可能會(huì)很慢.

                The latter will run subquery (whereHas) with nested subquery (where('id', function () {..}), so both ways might be potentially slow on big table.

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

                【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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 可滾動(dòng)游標(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ū)動(dòng)程序)
                    <tbody id='fuOrm'></tbody>
                • <i id='fuOrm'><tr id='fuOrm'><dt id='fuOrm'><q id='fuOrm'><span id='fuOrm'><b id='fuOrm'><form id='fuOrm'><ins id='fuOrm'></ins><ul id='fuOrm'></ul><sub id='fuOrm'></sub></form><legend id='fuOrm'></legend><bdo id='fuOrm'><pre id='fuOrm'><center id='fuOrm'></center></pre></bdo></b><th id='fuOrm'></th></span></q></dt></tr></i><div class="ynxsrhr" id='fuOrm'><tfoot id='fuOrm'></tfoot><dl id='fuOrm'><fieldset id='fuOrm'></fieldset></dl></div>

                    <bdo id='fuOrm'></bdo><ul id='fuOrm'></ul>
                      <legend id='fuOrm'><style id='fuOrm'><dir id='fuOrm'><q id='fuOrm'></q></dir></style></legend>

                    1. <small id='fuOrm'></small><noframes id='fuOrm'>

                        <tfoot id='fuOrm'></tfoot>

                          主站蜘蛛池模板: 酸度计_PH计_特斯拉计-西安云仪 纯水电导率测定仪-万用气体检测仪-低钠测定仪-米沃奇科技(北京)有限公司www.milwaukeeinst.cn | 无菌实验室规划装修设计-一体化实验室承包-北京洁净净化工程建设施工-北京航天科恩实验室装备工程技术有限公司 | 广西绿桂涂料--承接隔热涂料、隔音涂料、真石漆、多彩仿石漆等涂料工程双包施工 | 山东石英砂过滤器,除氟过滤器「价格低」-淄博胜达水处理 | 安平县鑫川金属丝网制品有限公司,声屏障,高速声屏障,百叶孔声屏障,大弧形声屏障,凹凸穿孔声屏障,铁路声屏障,顶部弧形声屏障,玻璃钢吸音板 | 苹果售后维修点查询,苹果iPhone授权售后维修服务中心 – 修果网 拼装地板,悬浮地板厂家,悬浮式拼装运动地板-石家庄博超地板科技有限公司 | 中天寰创-内蒙古钢结构厂家|门式刚架|钢结构桁架|钢结构框架|包头钢结构煤棚 | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | 模具硅橡胶,人体硅胶,移印硅胶浆厂家-宏图硅胶科技 | 点胶机_点胶阀_自动点胶机_智能点胶机_喷胶机_点胶机厂家【欧力克斯】 | 建筑消防设施检测系统检测箱-电梯**检测仪器箱-北京宇成伟业科技有限责任公司 | 恒温恒湿试验箱厂家-高低温试验箱维修价格_东莞环仪仪器_东莞环仪仪器 | 煤棒机_增碳剂颗粒机_活性炭颗粒机_木炭粉成型机-巩义市老城振华机械厂 | 辐射仪|辐射检测仪|辐射巡测仪|个人剂量报警仪|表面污染检测仪|辐射报警仪|辐射防护网 | 有福网(yofus.com)洗照片冲印,毕业聚会纪念册相册制作个性DIY平台 | 车充外壳,车载充电器外壳,车载点烟器外壳,点烟器连接头,旅行充充电器外壳,手机充电器外壳,深圳市华科达塑胶五金有限公司 | 便携式高压氧舱-微压氧舱-核生化洗消系统-公众洗消站-洗消帐篷-北京利盟救援 | 原子吸收设备-国产分光光度计-光谱分光光度计-上海光谱仪器有限公司 | 涿州网站建设_网站设计_网站制作_做网站_固安良言多米网络公司 | HDPE储罐_厂家-山东九州阿丽贝防腐设备 | 东莞市天进机械有限公司-钉箱机-粘箱机-糊箱机-打钉机认准东莞天进机械-厂家直供更放心! | 健康管理师报名入口,2025年健康管理师考试时间信息网-网站首页 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | 四合院设计_四合院装修_四合院会所设计-四合院古建设计与建造中心1 | 液氨泵,液化气泵-淄博「亚泰」燃气设备制造有限公司 | 无菌实验室规划装修设计-一体化实验室承包-北京洁净净化工程建设施工-北京航天科恩实验室装备工程技术有限公司 | 手表腕表维修保养鉴定售后服务中心网点 - 名表维修保养 | 雾度仪_雾度计_透光率雾度仪价格-三恩时(3nh)光电雾度仪厂家 | 合肥通道闸-安徽车牌识别-人脸识别系统厂家-安徽熵控智能技术有限公司 | 重庆钣金加工厂家首页-专业定做监控电视墙_操作台 | 北京普辉律师事务所官网_北京律师24小时免费咨询|法律咨询 | 电动高压冲洗车_价格-江苏速利达机车有限公司 | 拉力机-万能试验机-材料拉伸试验机-电子拉力机-拉力试验机厂家-冲击试验机-苏州皖仪实验仪器有限公司 | 解放卡车|出口|济南重汽|报价大全|山东三维商贸有限公司 | 即用型透析袋,透析袋夹子,药敏纸片,L型涂布棒-上海桥星贸易有限公司 | 杭州|上海贴标机-百科 | 赛尔特智能移动阳光房-阳光房厂家-赛尔特建筑科技(广东)有限公司 | 便携式XPDM露点仪-在线式防爆露点仪-增强型烟气分析仪-约克仪器 冰雕-冰雪世界-大型冰雕展制作公司-赛北冰雕官网 | 厦门ISO认证|厦门ISO9001认证|厦门ISO14001认证|厦门ISO45001认证-艾索咨询专注ISO认证行业 | 天津暖气片厂家_钢制散热器_天津铜铝复合暖气片_维尼罗散热器 | 生产加气砖设备厂家很多,杜甫机械加气砖设备价格公道 |