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

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

      2. <tfoot id='Q2vh0'></tfoot><legend id='Q2vh0'><style id='Q2vh0'><dir id='Q2vh0'><q id='Q2vh0'></q></dir></style></legend>
      3. Laravel Eloquent/Query Builder 中 LEFT JOIN 的 ON 子句中的

        时间:2023-09-22

      4. <legend id='hKQjx'><style id='hKQjx'><dir id='hKQjx'><q id='hKQjx'></q></dir></style></legend>
        <i id='hKQjx'><tr id='hKQjx'><dt id='hKQjx'><q id='hKQjx'><span id='hKQjx'><b id='hKQjx'><form id='hKQjx'><ins id='hKQjx'></ins><ul id='hKQjx'></ul><sub id='hKQjx'></sub></form><legend id='hKQjx'></legend><bdo id='hKQjx'><pre id='hKQjx'><center id='hKQjx'></center></pre></bdo></b><th id='hKQjx'></th></span></q></dt></tr></i><div id='hKQjx'><tfoot id='hKQjx'></tfoot><dl id='hKQjx'><fieldset id='hKQjx'></fieldset></dl></div>

            <bdo id='hKQjx'></bdo><ul id='hKQjx'></ul>

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

                  <tbody id='hKQjx'></tbody>
              1. <tfoot id='hKQjx'></tfoot>
                  本文介绍了Laravel Eloquent/Query Builder 中 LEFT JOIN 的 ON 子句中的参数化查询绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  假设我想显示 type="color" 的完整奖项列表:

                  Let's say I want to show a full list of awards with type="color":

                  Awards        Type     2013 Winner
                  ======        ====     ===========
                  Blue Award    color       Tom
                  Red Award     color
                  Green Award   color       Dan  
                  

                  为了达到这个结果,我可以在 Laravel 中进行这样的查询:

                  To achieve this result I could have a query in Laravel like this:

                  $year = '2013';
                  
                  $awards = DB::table('awards')
                               ->leftJoin('winners', function($join) use ($year)
                                     {
                                          $join->on('awards.id','=','winners.award_id');
                                          $join->on('winners.year','=',DB::raw("'".$year."'"));
                                     }
                               ->where('awards.type','color')
                               ->get();
                  

                  如果您输出 Laravel 生成的 SQL,您将看到只有 WHERE 子句 被参数化,并且 ON 子句 中的 $year 容易受到 sql 注入,如果我从不受信任的来源获取它.此外,查询的缓存潜力也会降低,因为 $year 会经常更改.注意:如果您认为我只是将第二个左连接条件添加到查询的 WHERE,这些不一样.

                  If you output the SQL that Laravel generates you will see that only the WHERE clause is parameterized and $year in the ON clause is left vulnerable to sql injection if I get it from an untrusted source. Also the query's caching potential is reduced because $year will change often. Note: In case you were thinking that I just add the second left join condition to the WHERE of the query, these are not the same.

                  关于如何将查询的 $year 部分参数化有什么想法吗?

                  Any ideas on how to get the $year part of the query parameterized?

                  推荐答案

                  这里有一个奇怪的解决方法(不想扩展 Builder 和 JoinClause 类):
                  注意:这会破坏 -> 的查询链,所以请注意 where 在下面被分隔.

                  Here's an odd work-around (didn't want to extend the Builder and JoinClause classes):
                  Notice: This will break query chaining with -> so notice the where was seperated below.

                  $query = DB::table('awards')
                           ->leftJoin('winners', function($join)
                                 {
                                      $join->on('awards.id','=','winners.award_id');
                                      $join->on('winners.year','=',DB::raw('?'));  
                                 }
                           ->setBindings(array_merge($query->getBindings(),array($year)));
                  
                  $query->where('awards.type','color');
                  
                  $awards = $query->get();
                  

                  更新:泰勒添加 joinWhere, leftJoinWhere... 他说如果你有一个函数连接,只需使用 ->where->orWhere从封闭内."不过我还没有尝试过.

                  UPDATE: Taylor added joinWhere, leftJoinWhere... he says that "if you have a function join just use ->where and ->orWhere from within the Closure." I've yet to try this though.

                  这篇关于Laravel Eloquent/Query Builder 中 LEFT JOIN 的 ON 子句中的参数化查询绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Xdebug 和 Netbeans 问题 下一篇:如何在数据库的同一列中保存多个行的输入?

                  相关文章

                  最新文章

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

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

                1. <legend id='Ig76s'><style id='Ig76s'><dir id='Ig76s'><q id='Ig76s'></q></dir></style></legend>

                  1. <tfoot id='Ig76s'></tfoot>

                        <bdo id='Ig76s'></bdo><ul id='Ig76s'></ul>