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

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

      • <bdo id='gq2AG'></bdo><ul id='gq2AG'></ul>
      <tfoot id='gq2AG'></tfoot>

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

      1. Eloquent 嵌套 whereHas

        Eloquent with nested whereHas(Eloquent 嵌套 whereHas)
        <legend id='jDUYg'><style id='jDUYg'><dir id='jDUYg'><q id='jDUYg'></q></dir></style></legend>

        1. <i id='jDUYg'><tr id='jDUYg'><dt id='jDUYg'><q id='jDUYg'><span id='jDUYg'><b id='jDUYg'><form id='jDUYg'><ins id='jDUYg'></ins><ul id='jDUYg'></ul><sub id='jDUYg'></sub></form><legend id='jDUYg'></legend><bdo id='jDUYg'><pre id='jDUYg'><center id='jDUYg'></center></pre></bdo></b><th id='jDUYg'></th></span></q></dt></tr></i><div class="66sysy4" id='jDUYg'><tfoot id='jDUYg'></tfoot><dl id='jDUYg'><fieldset id='jDUYg'></fieldset></dl></div>
            <tbody id='jDUYg'></tbody>

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

              <tfoot id='jDUYg'></tfoot>

                • <bdo id='jDUYg'></bdo><ul id='jDUYg'></ul>
                  本文介紹了Eloquent 嵌套 whereHas的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  目前我在我的模型集合中有這個 whereHas:

                  $query = self::whereHas('club', function($q) use ($search){$q->whereHas('owner', function($q) use ($search){$q->where('name', 'LIKE', '%'. $search .'%');});});

                  我的印象是上面的代碼可能是這樣的:

                  $query = self::whereHas('club.owner', function($q) use ($search){$q->where('name', 'LIKE', '%'. $search .'%');});

                  我知道這已經(jīng)很強(qiáng)大了,但即便如此,如果我有一個 5 層深的嵌套關(guān)系,事情就會變得很糟糕.

                  更新:

                  正如評論中所述,我最終沒有說清楚我的問題,我深表歉意.

                  我將嘗試使用一個簡單的例子,考慮$owner->club->membership->product->package,現(xiàn)在從所有者我想搜索某個包,應(yīng)該是這樣的:

                  $query = self::whereHas('club', function($q) use ($search){$q->whereHas('membership', function($q) use ($search){$q->whereHas('product', function($q) use ($search){$q->whereHas('package', function($q) use ($search){$q->where('alias', 'LIKE', '%'. $search .'%');});//包裹});//產(chǎn)品});//會員資格});//俱樂部

                  這是正確的嗎?有捷徑嗎?

                  解決方案

                  更新:PR 剛剛合并到 4.2,所以現(xiàn)在可以在 點(diǎn)嵌套 表示法>has 方法 ( ->has('relation1.relation2) ->whereHas('relation1.relation2, .. )

                  您的問題仍然有點(diǎn)不清楚,或者您誤解了 whereHas() 方法,因為它用于過濾模型(在本例中為用戶)并僅獲取那些具有符合搜索條件的相關(guān)模型的模型.>

                  看來您想從給定的用戶的上下文中查找,所以不需要使用 whereHas 方法.

                  無論如何,取決于關(guān)系(1-1,1-m,m-m),這可能很容易,也可能非常困難,而且效率不高.正如我所說,加載嵌套關(guān)系意味著對于每一級嵌套都會出現(xiàn)另一個 db 查詢,因此在這種情況下,您最終會得到 5 個查詢.

                  無論關(guān)系如何,您都可以像這樣反轉(zhuǎn)此鏈,因為這樣會更容易:

                  <小時>

                  這不會在 atm 工作,因為 whereHas() 不處理點(diǎn)嵌套關(guān)系!

                  //給定 $user 和 $search:$packages = Package::where('alias','like',"%$search%")->whereHas('product.membership.club.user', function ($q) use ($user) {$q->whereId($user->id);})->get();

                  <小時>

                  如您所見,這更具可讀性,但仍運(yùn)行 5 個查詢.通過這種方式,您可以獲得 $packages,這是您想要的模型的單個集合.

                  雖然從用戶的上下文中你會得到這樣的東西(再次取決于關(guān)系):

                  $user|-俱樂部||-會員|||-產(chǎn)品||||-包|||-另一個產(chǎn)品||||-包|||-又一個產(chǎn)品|||-包||-另一個會員.....

                  你明白了,不是嗎?

                  您可以從 Collection 中獲取包,但這會很麻煩.反過來更容易.

                  所以你的問題的答案就是加入表格:

                  //讓我們假設(shè)關(guān)系是最容易處理的:1-many$packages = Package::where('alias','like',"%$search%")->join('products','packages.product_id','=','products.id')->join('memberships','products.membership_id','=','memberships.id')->join('clubs','memberships.club_id','=','clubs.id')->where('clubs.user_id','=',$user->id)->get(['packages.*']);//除了包表外不要選擇任何東西

                  當(dāng)然你可以用一個很好的方法包裝它,這樣你就不必每次執(zhí)行這樣的搜索時都寫這個.這個查詢的性能肯定會比上面顯示的單獨(dú) 5 個查詢好得多.顯然這種方式你只加載包,沒有其他相關(guān)的模型.

                  Currently I have this whereHas in a collection of my model:

                  $query = self::whereHas('club', function($q) use ($search)
                  {
                      $q->whereHas('owner', function($q) use ($search)
                      {
                          $q->where('name', 'LIKE', '%'. $search .'%');
                      });
                  
                  });
                  

                  I was under the impression the code above could be as such:

                  $query = self::whereHas('club.owner', function($q) use ($search)
                  {
                      $q->where('name', 'LIKE', '%'. $search .'%');    
                  });
                  

                  I'm aware this is already a lot of power, but even then, if I have a nested relationship 5 levels deep, things will get ugly.

                  Update:

                  As stated in the comments, I ended up not making my question clear, I apologize.

                  I will try to use a simple example, consider $owner->club->membership->product->package, now from owners I want to search a certain package, it would be something like this:

                  $query = self::whereHas('club', function($q) use ($search)
                  {
                      $q->whereHas('membership', function($q) use ($search)
                      {
                          $q->whereHas('product', function($q) use ($search)
                          {
                              $q->whereHas('package', function($q) use ($search)
                              {
                                  $q->where('alias', 'LIKE', '%'. $search .'%');
                              });//package
                          });//product
                      });//membership
                  });//club
                  

                  Is this correct? Is there a shortcut?

                  解決方案

                  Update: the PR has been just merged to 4.2, so now it's possible to use dot nested notation in has methods ( ->has('relation1.relation2) ->whereHas('relation1.relation2, .. )

                  Your question remains a bit unclear or you misunderstand whereHas() method as it is used to filter models (users in this case) and get only those that have related models fitting search conditions.

                  It seems that you want to find Packages from the context of a given User, so no need to use whereHas method.

                  Anyway depending on the relations (1-1,1-m,m-m) this can be easy or pretty hard and not very efficient. As I stated, loading nested relations means that for every level of nesting comes another db query, so in this case you end up with 5 queries.

                  Regardless of the relations you can invert this chain like this, as it will be easier:


                  edit: This is not going to work atm as whereHas() doesn't process dot nested relations!

                  // given $user and $search:
                  $packages = Package::where('alias','like',"%$search%")
                    ->whereHas('product.membership.club.user', function ($q) use ($user) {
                      $q->whereId($user->id);
                    })->get();
                  


                  As you can see this is much more readable, still runs 5 queries. Also this way you get $packages, which is a single Collection of the models you wanted.

                  While from the context of a user you would get something like this (depending on the relations again):

                  $user
                   |-club
                   |  |-membership
                   |  |  |-product
                   |  |  |  |-packages
                   |  |  |-anotherProduct
                   |  |  |  |-packages
                   |  |  |-yetAnotherProduct
                   |  |     |-packages
                   |  |-anotherMembership
                    .....
                  

                  You get the point, don't you?

                  You could fetch the packages from the Collection, but it would be cumbersome. It's easier the other way around.

                  So the answer to your question would be simply joining the tables:

                  // Let's assume the relations are the easiest to handle: 1-many
                  $packages = Package::where('alias','like',"%$search%")
                    ->join('products','packages.product_id','=','products.id')
                    ->join('memberships','products.membership_id','=','memberships.id')
                    ->join('clubs','memberships.club_id','=','clubs.id')
                    ->where('clubs.user_id','=',$user->id)
                    ->get(['packages.*']); // dont select anything but packages table 
                  

                  Of course you can wrap it in a nice method so you don't have to write this everytime you perform such search. Performance of this query will be definitely much better than separate 5 queries shown above. Obviously this way you load only packages, without other related models.

                  這篇關(guān)于Eloquent 嵌套 whereHas的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅(qū)動程序)
                      <bdo id='JzEg1'></bdo><ul id='JzEg1'></ul>
                        <tbody id='JzEg1'></tbody>
                      • <legend id='JzEg1'><style id='JzEg1'><dir id='JzEg1'><q id='JzEg1'></q></dir></style></legend>

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

                            <tfoot id='JzEg1'></tfoot>
                          • <i id='JzEg1'><tr id='JzEg1'><dt id='JzEg1'><q id='JzEg1'><span id='JzEg1'><b id='JzEg1'><form id='JzEg1'><ins id='JzEg1'></ins><ul id='JzEg1'></ul><sub id='JzEg1'></sub></form><legend id='JzEg1'></legend><bdo id='JzEg1'><pre id='JzEg1'><center id='JzEg1'></center></pre></bdo></b><th id='JzEg1'></th></span></q></dt></tr></i><div class="ywg2ckw" id='JzEg1'><tfoot id='JzEg1'></tfoot><dl id='JzEg1'><fieldset id='JzEg1'></fieldset></dl></div>
                            主站蜘蛛池模板: 中原网视台| 丹佛斯变频器-丹佛斯压力开关-变送器-广州市风华机电设备有限公司 | 工业CT-无锡璟能智能仪器有限公司 | 科普仪器菏泽市教育教学仪器总厂| 石英陶瓷,石英坩埚,二氧化硅陶瓷-淄博百特高新材料有限公司 | 自进式锚杆-自钻式中空注浆锚杆-洛阳恒诺锚固锚杆生产厂家 | 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 400电话_400电话申请_888元包年_400电话办理服务中心_400VIP网 | 基本型顶空进样器-全自动热脱附解吸仪价格-AutoHS全模式-成都科林分析技术有限公司 | 阿尔法-MDR2000无转子硫化仪-STM566 SATRA拉力试验机-青岛阿尔法仪器有限公司 | 塑料异型材_PVC异型材_封边条生产厂家_PC灯罩_防撞扶手_医院扶手价格_东莞市怡美塑胶制品有限公司 | T恤衫定做,企业文化衫制作订做,广告T恤POLO衫定制厂家[源头工厂]-【汉诚T恤定制网】 | 小型铜米机-干式铜米机-杂线全自动铜米机-河南鑫世昌机械制造有限公司 | 土壤检测仪器_行星式球磨仪_土壤团粒分析仪厂家_山东莱恩德智能科技有限公司 | 切铝机-数控切割机-型材切割机-铝型材切割机-【昆山邓氏精密机械有限公司】 | 浴室柜-浴室镜厂家-YINAISI · 意大利设计师品牌 | 咿耐斯 |-浙江台州市丰源卫浴有限公司 | 齿辊分级破碎机,高低压压球机,立式双动力磨粉机-郑州长城冶金设备有限公司 | 皮带机-带式输送机价格-固定式胶带机生产厂家-河南坤威机械 | 家德利门业,家居安全门,别墅大门 - 安徽家德利门业有限公司 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 影合社-影视人的内容合作平台 | 等离子表面处理机-等离子表面活化机-真空等离子清洗机-深圳市东信高科自动化设备有限公司 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | 酸度计_PH计_特斯拉计-西安云仪 纯水电导率测定仪-万用气体检测仪-低钠测定仪-米沃奇科技(北京)有限公司www.milwaukeeinst.cn | 杭州公司变更法人-代理记账收费价格-公司注销代办_杭州福道财务管理咨询有限公司 | 煤矿人员精确定位系统_矿用无线通信系统_煤矿广播系统 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 书法培训-高考书法艺考培训班-山东艺霖书法培训凭实力挺进央美 | MTK核心板|MTK开发板|MTK模块|4G核心板|4G模块|5G核心板|5G模块|安卓核心板|安卓模块|高通核心板-深圳市新移科技有限公司 | 沧州友城管业有限公司-内外涂塑钢管-大口径螺旋钢管-涂塑螺旋管-保温钢管生产厂家 | 步进电机_agv电机_伺服马达-伺服轮毂电机-和利时电机 | 电地暖-电采暖-发热膜-石墨烯电热膜品牌加盟-暖季地暖厂家 | 磁力抛光机_磁力研磨机_磁力去毛刺机_精密五金零件抛光设备厂家-冠古科技 | 硅胶布|电磁炉垫片|特氟龙胶带-江苏浩天复合材料有限公司 | 便民信息网_家电维修,家电清洗,开锁换锁,本地家政公司 | AR开发公司_AR增强现实_AR工业_AR巡检|上海集英科技 | 智能门锁电机_智能门锁离合器_智能门锁电机厂家-温州劲力智能科技有限公司 | 塑胶地板-商用PVC地板-pvc地板革-安耐宝pvc塑胶地板厂家 | 北京三友信电子科技有限公司-ETC高速自动栏杆机|ETC机柜|激光车辆轮廓测量仪|嵌入式车道控制器 | 苏州西装定制-西服定制厂家-职业装定制厂家-尺品服饰西装定做公司 | 上海防爆真空干燥箱-上海防爆冷库-上海防爆冷柜?-上海浦下防爆设备厂家? |