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

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

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

        Laravel - 根據動態參數擴展 Eloquent where 子句

        Laravel - extending Eloquent where clauses depending on dynamic parameters(Laravel - 根據動態參數擴展 Eloquent where 子句)

          <tbody id='YcP88'></tbody>

          • <bdo id='YcP88'></bdo><ul id='YcP88'></ul>
          • <tfoot id='YcP88'></tfoot>
              • <legend id='YcP88'><style id='YcP88'><dir id='YcP88'><q id='YcP88'></q></dir></style></legend>

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

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

                • 本文介紹了Laravel - 根據動態參數擴展 Eloquent where 子句的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想根據我從 json 對象收集的搜索參數構建一系列雄辯的 WHERE 子句.

                  I would like to construct a series of eloquent WHERE clauses dependent on the search parameters I collect from a json object.

                  像這樣的東西(不管對象的語法,,,它是一種解釋,只是為了演示):

                  Something like this (never mind the syntax of object,,, it is an interpretation only to demonstrate):

                  $searchmap = "
                  {
                      "color": "red",
                      "height": "1",
                      "width": "2",
                      "weight": "",
                      "size": "",
                  }";
                  

                  然后我取對象并解碼以獲得搜索數組...

                  I then take the object and decode to get a search array...

                  $search = json_decode($searchmap, true);
                  

                  如果我的權重和大小設置為 null 或者是一個空字符串",我會有像這樣的雄辯代碼..

                  If my weight and size are set to null or are an 'empty string' I would have eloquent code that looks like this..

                  $gadgets = Gadget::where('color',   '=', $search['color'])
                                   ->where('height',  '=', $search['height'])
                                   ->where('width',   '=', $search['width'])
                                   ->paginate(9);
                  

                  如果它們有一個值,那么雄辯的代碼看起來像這樣..

                  If they have a value then eloquent code would look like this..

                  $gadgets = Gadget::where('color',   '=', $search['color'])
                                   ->where('height',  '=', $search['height'])
                                   ->where('width',   '=', $search['width'])
                                   ->where('weight',  '=', $search['weight'])
                                   ->where('size',    '=', $search['size'])
                                   ->paginate(9);
                  

                  有沒有辦法動態地實現這一點.

                  Is there a way to accomplish this dynamically.

                  我想問題應該在于有沒有辦法根據給定的參數動態鏈接雄辯的 where 子句?

                  I suppose the question should be ins there a way to chain eloquent where clauses dynamically based on a given parameter?

                  在偽上下文中,我希望做這樣的事情

                  In a pseudo context I am looking to do something like this

                  $gadgets = Gadget::
                  
                      foreach ($search as $key => $parameter) {
                          if ( $parameter <> '' ) {
                              ->where($key, '=', $parameter)
                          }
                      }
                  
                  ->paginate(9);
                  

                  是否可以以類似于此的方式創建 where 子句的鏈接?

                  Can chaining of where clauses be created in some way similar to this?

                  感謝您花時間看這個!

                  更新:

                  我也想出了類似這樣的東西,似乎效果很好,但如果改進是個好主意,我歡迎提出建議.

                  I also came up with something like this that seems to work well but i would like to welcome suggestions if improvement is a good idea.

                  $gadgets = New Gadget();
                      foreach ($search as $key => $parameter) {
                          if($parameter != ''){
                              $gadgets = $gadgets->where($key, '=', $parameter);
                          }
                      }
                  $gadgets = $gadgets->paginate(9);
                  

                  <小時>

                  最終版

                  感謝下面的@lukasgeiter,我想我會選擇這個

                  And thanks to @lukasgeiter below I think I will go with this

                  $gadgets = Gadget::whereNested(function($query) use ($search) {
                      foreach ($search as $key => $value)
                          {
                              if($value != ''){
                                  $query->where($key, '=', $value);
                              }
                          }
                  }, 'and');
                  $gadgets = $gadgets->paginate(9);
                  

                  推薦答案

                  這很簡單.Laravel 的 where 函數允許你傳入一個鍵值對數組.

                  That's easy. Laravel's where function allows you to pass in an array of key value pairs.

                  $searchmap = array(
                      'color' => 'red',
                      'height' => '1'
                      // etc
                  );
                  
                  $gadgets = Gadget::where($searchmap)->paginate(9);
                  

                  如果你好奇,那是源代碼的相關部分 (IlluminateDatabaseQueryBuilder)

                  If you are curious, that's the relevant part of the source (IlluminateDatabaseQueryBuilder)

                  public function where($column, $operator = null, $value = null, $boolean = 'and')
                  {
                      // If the column is an array, we will assume it is an array of key-value pairs
                      // and can add them each as a where clause. We will maintain the boolean we
                      // received when the method was called and pass it into the nested where.
                      if (is_array($column))
                      {
                          return $this->whereNested(function($query) use ($column)
                          {
                              foreach ($column as $key => $value)
                              {
                                  $query->where($key, '=', $value);
                              }
                          }, $boolean);
                      }
                  
                      // many more lines of code....
                  }
                  

                  編輯

                  要對其進行更多控制(例如,將="更改為另一個比較運算符),請嘗試直接使用 Laravel 內部使用的代碼:

                  Edit

                  To have more control over it (e.g. changing the "=" to another comparison operator) try using the code laravel uses internally directly:

                  $gadgets = Gadget::whereNested(function($query) use ($searchmap)
                          {
                              foreach ($searchmap as $key => $value)
                              {
                                  if($value != ''){
                                      $query->where($key, '=', $value);
                                  }
                              }
                          }, 'and')->paginate(9);
                  

                  這篇關于Laravel - 根據動態參數擴展 Eloquent where 子句的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='mbb8V'></tbody>
                  <i id='mbb8V'><tr id='mbb8V'><dt id='mbb8V'><q id='mbb8V'><span id='mbb8V'><b id='mbb8V'><form id='mbb8V'><ins id='mbb8V'></ins><ul id='mbb8V'></ul><sub id='mbb8V'></sub></form><legend id='mbb8V'></legend><bdo id='mbb8V'><pre id='mbb8V'><center id='mbb8V'></center></pre></bdo></b><th id='mbb8V'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='mbb8V'><tfoot id='mbb8V'></tfoot><dl id='mbb8V'><fieldset id='mbb8V'></fieldset></dl></div>

                • <tfoot id='mbb8V'></tfoot>

                  1. <legend id='mbb8V'><style id='mbb8V'><dir id='mbb8V'><q id='mbb8V'></q></dir></style></legend>
                      <bdo id='mbb8V'></bdo><ul id='mbb8V'></ul>
                          • <small id='mbb8V'></small><noframes id='mbb8V'>

                          • 主站蜘蛛池模板: 81精品国产乱码久久久久久 | 免费中文字幕日韩欧美 | 日韩免费 | 亚洲欧美另类在线 | 亚洲视频三区 | 亚洲精品日韩在线 | 久久国产精品99久久久久 | 亚洲一区二区在线播放 | 国产精品一区久久久久 | 国产一区欧美一区 | 91视频亚洲 | 黄色高清视频 | 综合二区 | 热久久久久 | 男人天堂网av | 欧美11一13sex性hd| 国产精品免费一区二区三区四区 | 国产91精品久久久久久久网曝门 | 一色一黄视频 | 国产成人综合av | 欧美最猛性xxxxx亚洲精品 | 操久久久 | 日本久久精品视频 | 成人3d动漫一区二区三区91 | 国产亚洲精品精品国产亚洲综合 | 拍拍无遮挡人做人爱视频免费观看 | 欧美精品1区2区3区 精品国产欧美一区二区 | 国产三区在线观看视频 | 嫩草视频在线 | 亚洲久草| 国产视频2021 | 久久天天躁狠狠躁夜夜躁2014 | 国产男女猛烈无遮掩视频免费网站 | 日本视频在线播放 | 精品久久99| 欧美午夜精品理论片a级按摩 | 亚洲社区在线 | 色吧色综合 | 欧美黄色性生活视频 | 午夜精品一区二区三区在线视 | 国产精品伦一区二区三级视频 |