<tfoot id='Bpz8t'></tfoot>

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

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

        Laravel 查询构建器参数绑定

        时间:2023-09-23

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

                  <tbody id='R4Ykq'></tbody>

                • <legend id='R4Ykq'><style id='R4Ykq'><dir id='R4Ykq'><q id='R4Ykq'></q></dir></style></legend>

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

                  <i id='R4Ykq'><tr id='R4Ykq'><dt id='R4Ykq'><q id='R4Ykq'><span id='R4Ykq'><b id='R4Ykq'><form id='R4Ykq'><ins id='R4Ykq'></ins><ul id='R4Ykq'></ul><sub id='R4Ykq'></sub></form><legend id='R4Ykq'></legend><bdo id='R4Ykq'><pre id='R4Ykq'><center id='R4Ykq'></center></pre></bdo></b><th id='R4Ykq'></th></span></q></dt></tr></i><div id='R4Ykq'><tfoot id='R4Ykq'></tfoot><dl id='R4Ykq'><fieldset id='R4Ykq'></fieldset></dl></div>
                  本文介绍了Laravel 查询构建器参数绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试将相同的值绑定到原始查询 (Laravel 5.2) 中的某个参数

                  I'm trying to bind the same value to some parameter in a raw query (Laravel 5.2)

                  //this is a non practical example ,only for clarify the question
                  
                  DB::table('users as u')
                  ->select('id')
                  ->whereRaw('u.id > ? or u.id < ? or u.id = ?',[2,2,2])
                  ->first();
                  

                  有没有办法一次性绑定相同的参数(防止[2,2,2]中的值重复)?

                  is there any way to bind the same parameters at once(prevent duplicating values in [2,2,2])?

                  推荐答案

                  使用命名参数.它们包含在数据库页面的运行原始 SQL 查询部分,在副标题使用命名绑定下.引用:

                  Use named parameters. They're covered in the documentation in the Running Raw SQL Queries section of the Database page, under the subheading Using Named Bindings. Quoting:

                  您可以使用命名绑定执行查询,而不是使用 ? 来表示您的参数绑定:

                  Instead of using ? to represent your parameter bindings, you may execute a query using named bindings:

                  $results = DB::select('select * from users where id = :id', ['id' => 1]);
                  

                  在你的情况下,你应该能够运行这个:

                  In your case you ought to be able to run this:

                  DB::table('users as u')
                      ->select('id')
                      ->whereRaw('u.id > :id or u.id < :id or u.id = :id', [
                          'id' => 2,
                      ])
                      ->first();
                  

                  但似乎 Laravel 抛出了一个 QueryException 消息Invalid parameter number.我已将此报告为一个错误.

                  But it seems Laravel throws a QueryException with the message Invalid parameter number. I've reported this as a bug.

                  如果您真的想使用 whereRaw,您可以改为从变量构建参数数组:

                  If you really want to use whereRaw you could instead build your array of parameters from a variable:

                  $id = 2;
                  DB::table('users as u')
                      ->select('id')
                      ->whereRaw('u.id > ? or u.id < ? or u.id = ?', [
                          $id, $id, $id,
                      ])
                      ->first();
                  

                  或者使用array_fill重复对您的价值:

                  Or use array_fill to repeat the value for you:

                  $id = 2;
                  DB::table('users as u')
                      ->select('id')
                      ->whereRaw('u.id > ? or u.id < ? or u.id = ?', array_fill(0, 3, $id))
                      ->first();
                  

                  如果您不需要 whereRaw,您可以使用查询构建器的其他功能并一点一点地构建查询,参数来自变量:

                  If you don't need whereRaw you can instead use other features of the query builder and build the query bit by bit, with the parameter coming from a variable:

                  $id = 2;
                  DB::table('users')
                      ->select('id')
                      ->where('id', '>', $id)
                      ->orWhere('id', '<', $id)
                      ->orWhere('id', $id)
                      ->first();
                  

                  查询构建器非常强大,为了获得更复杂的逻辑,您可以嵌套闭包.有关示例,请参阅文档的相关部分.

                  The query builder is quite powerful, and to get more complicated logic you can nest closures. See the relevant section of the docs for some examples.

                  这篇关于Laravel 查询构建器参数绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在没有 Git 的情况下读取 Git 存储库 下一篇:URL重写-重写代码

                  相关文章

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

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

                      <legend id='DVOi4'><style id='DVOi4'><dir id='DVOi4'><q id='DVOi4'></q></dir></style></legend>