<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 查询构建器参数绑定

        Laravel query builder parameter binding(Laravel 查询构建器参数绑定)

              • <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 查询构建器参数绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  <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>

                            <tbody id='DVOi4'></tbody>
                        • <small id='DVOi4'></small><noframes id='DVOi4'>

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