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

      <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="qwawimqqmiuu" 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="qwawimqqmiuu" 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 急切加載限制的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我有兩個表,比如users"和users_actions",其中users_actions"與用戶有 hasMany 關系:

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

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

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

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

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

                但我當然想避免這種情況,只為每個用戶選擇最后一個操作.

                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();
                

                但是這給了我一個錯誤的結果,因為 Laravel 執行這個查詢:

                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
                

                這當然是錯誤的.是否有可能在不使用 Eloquent 對每條記錄執行查詢的情況下獲得此信息?我是否犯了一些我沒有看到的明顯錯誤?我剛開始使用 Eloquent,有時關系會困擾我.

                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.

                代表目的的一部分,我還需要此功能來在關系中搜索,例如我想搜索 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);
                });
                

                但是這給了我所有有一個 action = 1 的用戶,因為它是一個日志,每個人都通過了這一步.


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


                感謝@berkayk 看起來我解決了問題的第一部分,但我仍然無法在關系中進行搜索.

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

                仍然沒有執行正確的查詢,它會生成如下內容:

                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 相關模型,我由 @berbayk 鏈接的解決方案很酷.

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

                但是,它無法解決您所要求的其他部分,因為使用 where 子句查詢這種關系會導致您已經經歷過的幾乎相同的結果 - 所有行都會被返回,只有 latest 實際上不會是最新的(但最新的匹配 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).

                給你:

                簡單方法 - 獲取所有并過濾集合:

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

                <小時>

                或困難的方式 - 在 sql 中進行(假設為 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();
                

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

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

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

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

                這篇關于Laravel 急切加載限制的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 找不到驅動程序)
                    <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="qwawimqqmiuu" 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>

                          主站蜘蛛池模板: 黄色毛片在线看 | 在线不卡视频 | 日本 欧美 三级 高清 视频 | 本地毛片| 天天操夜夜操 | 日韩一区二区在线播放 | 国产精品国产三级国产aⅴ原创 | 日韩一级 | 欧美日韩亚洲一区 | 国产在线一区观看 | 久久一区视频 | 久久国产精品视频 | 成人在线视频网站 | 亚洲欧美激情精品一区二区 | 精品成人免费一区二区在线播放 | 日韩在线观看精品 | 亚洲欧美日韩电影 | 草久在线 | 亚洲影音先锋 | 国产精品一区二区久久 | 国产视频久久久 | 欧美久久一区 | 999热精品视频 | 久草网址| 午夜精品一区二区三区免费视频 | 日韩不卡视频在线观看 | 亚洲国产一 | 天天操夜夜操免费视频 | 91五月婷蜜桃综合 | 91在线一区二区三区 | 激情麻豆视频 | 午夜寂寞影院列表 | 精品欧美一区二区精品久久 | 久久免费精品 | 剑来高清在线观看 | 久草网站 | 国产在视频一区二区三区吞精 | 在线观看黄色大片 | 99热都是精品| 亚洲欧美在线视频 | 男人的天堂久久 |