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

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

                  問題描述

                  目前我在我的模型集合中有這個 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 .'%');});

                  我知道這已經很強大了,但即便如此,如果我有一個 5 層深的嵌套關系,事情就會變得很糟糕.

                  更新:

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

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

                  $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 .'%');});//包裹});//產品});//會員資格});//俱樂部

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

                  解決方案

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

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

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

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

                  無論關系如何,您都可以像這樣反轉此鏈,因為這樣會更容易:

                  <小時>

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

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

                  <小時>

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

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

                  $user|-俱樂部||-會員|||-產品||||-包|||-另一個產品||||-包|||-又一個產品|||-包||-另一個會員.....

                  你明白了,不是嗎?

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

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

                  //讓我們假設關系是最容易處理的: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.*']);//除了包表外不要選擇任何東西

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

                  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.

                  這篇關于Eloquent 嵌套 whereHas的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 找不到驅動程序)
                      <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="qwawimqqmiuu" id='JzEg1'><tfoot id='JzEg1'></tfoot><dl id='JzEg1'><fieldset id='JzEg1'></fieldset></dl></div>
                            主站蜘蛛池模板: 天天干成人网 | 激情网站在线观看 | 一区二区三区精品在线视频 | 精品国产一区二区三区久久久久久 | 日本午夜精品 | 国产乱码一二三区精品 | 在线看免费| 国产一区高清 | 天天夜夜人人 | 欧美一二精品 | 99久久精品视频免费 | 久久久久亚洲 | 四虎影院美女 | 欧美一区二区精品 | 黄色精品 | 精品国产青草久久久久96 | 欧美性jizz18性欧美 | 亚洲高清在线视频 | 国产午夜精品一区二区三区在线观看 | 久久久久久国产精品mv | 国产传媒在线播放 | 久久免费精品 | 日本在线免费视频 | 国内精品伊人久久久久网站 | 三级av网址| 伊人精品在线视频 | 日本中出视频 | 天天爱爱网 | 国产精品久久亚洲 | 黄色大片免费网站 | 国产日韩精品一区二区 | 欧美色影院 | 黄色免费观看网站 | 成人免费视频一区 | 国产一区二区三区在线 | 久久伊人一区 | 精品日本中文字幕 | 日韩国产中文字幕 | 国产视频一区二区三区四区五区 | 欧美理论片在线观看 | 午夜大片 |