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

<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="a0gag0a" 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="emew0i0" 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="s2ywaqk" 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'>

                          • 主站蜘蛛池模板: 成都亚克力制品,PVC板,双色板雕刻加工,亚克力门牌,亚克力标牌,水晶字雕刻制作-零贰捌广告 | 诸城网站建设-网络推广-网站优化-阿里巴巴托管-诸城恒泰互联 | 塑木弯曲试验机_铜带拉伸强度试验机_拉压力测试台-倾技百科 | 安徽控制器-合肥船用空调控制器-合肥家电控制器-合肥迅驰电子厂 安徽净化板_合肥岩棉板厂家_玻镁板厂家_安徽科艺美洁净科技有限公司 | 健身器材-健身器材厂家专卖-上海七诚健身器材有限公司 | 合肥触摸一体机_触摸查询机厂家_合肥拼接屏-安徽迅博智能科技 | 防爆大气采样器-防爆粉尘采样器-金属粉尘及其化合物采样器-首页|盐城银河科技有限公司 | 硅胶制品-硅橡胶制品-东莞硅胶制品厂家-广东帝博科技有限公司 | 纸张环压仪-纸张平滑度仪-杭州纸邦自动化技术有限公司 | 杰恒蠕动泵-蠕动泵专业厂家-19年专注蠕动泵| 浙江浩盛阀门有限公司| 天空彩票天下彩,天空彩天空彩票免费资料,天空彩票与你同行开奖,天下彩正版资料大全 | 雷冲击高压发生器-水内冷直流高压发生器-串联谐振分压器-武汉特高压电力科技有限公司 | Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 英国公司注册-新加坡公司注册-香港公司开户-离岸公司账户-杭州商标注册-杭州优创企业 | 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 金属回收_废铜废铁回收_边角料回收_废不锈钢回收_废旧电缆线回收-广东益夫金属回收公司 | 精准猎取科技资讯,高效阅读科技新闻_科技猎 | 葡萄酒灌装机-食用油灌装机-液体肥灌装设备厂家_青州惠联灌装机械 | 氧化锆陶瓷_氧化锆陶瓷加工_氧化锆陶瓷生产厂家-康柏工业陶瓷有限公司 | 电缆桥架生产厂家_槽式/梯式_热镀锌线槽_广东东莞雷正电气 | 标准光源箱|对色灯箱|色差仪|光泽度仪|涂层测厚仪_HRC大品牌生产厂家 | 新疆系统集成_新疆系统集成公司_系统集成项目-新疆利成科技 | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 菲希尔X射线测厚仪-菲希尔库伦法测厚仪-无锡骏展仪器有限责任公司 | 杭州货架订做_组合货架公司_货位式货架_贯通式_重型仓储_工厂货架_货架销售厂家_杭州永诚货架有限公司 | 高空重型升降平台_高空液压举升平台_高空作业平台_移动式升降机-河南华鹰机械设备有限公司 | 工作服定制,工作服定做,工作服厂家-卡珀职业服装(苏州)有限公司 | 钢绞线万能材料试验机-全自动恒应力两用机-混凝土恒应力压力试验机-北京科达京威科技发展有限公司 | 牛皮纸|牛卡纸|进口牛皮纸|食品级牛皮纸|牛皮纸厂家-伽立实业 | 安全,主动,被动,柔性,山体滑坡,sns,钢丝绳,边坡,防护网,护栏网,围栏,栏杆,栅栏,厂家 - 护栏网防护网生产厂家 | 国产频谱分析仪-国产网络分析仪-上海坚融实业有限公司 | 粉末冶金-粉末冶金齿轮-粉末冶金零件厂家-东莞市正朗精密金属零件有限公司 | 橡胶粉碎机_橡胶磨粉机_轮胎粉碎机_轮胎磨粉机-河南鼎聚重工机械制造有限公司 | 创绿家招商加盟网-除甲醛加盟-甲醛治理加盟-室内除甲醛加盟-创绿家招商官网 | 臻知网大型互动问答社区-你的问题将在这里得到解答!-无锡据风网络科技有限公司 | 无压烧结银_有压烧结银_导电银胶_导电油墨_导电胶-善仁(浙江)新材料 | 钢格板|热镀锌钢格板|钢格栅板|钢格栅|格栅板-安平县昊泽丝网制品有限公司 | 换网器_自动换网器_液压换网器--郑州海科熔体泵有限公司 | 【灵硕展览集团】展台展会设计_展览会展台搭建_展览展示设计一站式服务公司 | 建筑资质代办-建筑资质转让找上海国信启航 |