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

    1. <tfoot id='tVGuW'></tfoot>
    2. <small id='tVGuW'></small><noframes id='tVGuW'>

        • <bdo id='tVGuW'></bdo><ul id='tVGuW'></ul>

      1. 如何获取 Laravel 删除/更新/插入语句的原始 SQL?

        时间:2023-09-22
      2. <legend id='luC3w'><style id='luC3w'><dir id='luC3w'><q id='luC3w'></q></dir></style></legend>

            <tfoot id='luC3w'></tfoot>

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

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

                    <tbody id='luC3w'></tbody>
                  本文介绍了如何获取 Laravel 删除/更新/插入语句的原始 SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我知道我可以在查询构建器上使用 toSql 方法来获取带有 SELECT 语句的绑定参数占位符的原始 SQL.

                  AppUser::where(['id'=>'1'])->toSql();

                  <块引用>

                  "select * from `users` where (`id` = ?)"

                  但是我怎样才能为 DELETE 语句得到这个?

                  AppUser::where(['id'=>'1'])->delete()->toSql();

                  <块引用>

                  PHP 错误:在第 1 行的整数上调用成员函数 toSql()

                  这将执行该语句,但我想获得原始的、未插值的 SQL,其中问号代表值实际运行查询.同样的情况适用于任何修改语句,例如 INSERTUPDATE

                  但是为什么,谁在乎呢?

                  这很像xy问题.我的 Web 应用程序包括一个多进程架构.它在更新数据库的事件驱动循环中运行带有异步通信侦听的自定义工匠命令.

                  我需要原始查询的原因是因为我想重用准备好的语句以提高性能.不幸的是,eloquent 方法没有公开准备好的语句,所以为了重用一个,我必须自己从底层 PDO 连接准备它.

                  $sql = 'UPDATE `foo` SET `bar` = ?WHERE (`id` = ?)';$statement = DB::connection()->getPdo()->prepare($sql);而(真){$data = foo();$statement->execute([$data->bar, $data->id]);}

                  然而,这与抽象的 SQL 语法构建器不同.因为我目前正在使用 MySQL,所以我可以在语法上包含反引号.但现在我陷入了供应商锁定的困境.例如,老板说我们明天要迁移到 MS SQL Server,那么(至少)必须捕获使用反引号而不是方括号的错误可能会很烦人.

                  我想使用动态生成的 SQL 语法在准备好的语句中重复使用.

                  解决方案

                  首先获取模型表的查询构建器实例.

                  $builder = DB::table((new User)->getTable());

                  然后获取语法并使用 where 子句从构建器编译删除语句.

                  $sql = $builder->getGrammar()->compileDelete($builder->where('id', 1));

                  <块引用>

                  从 `users` 中删除 `id` = ?"

                  现在您可以自由地更换数据库驱动程序,并且仍然可以获得适当的平台语法.

                  I know I can use the toSql method on a query builder to get the raw SQL with binding parameter placeholders for a SELECT statement.

                  AppUser::where(['id'=>'1'])->toSql();
                  

                  "select * from `users` where (`id` = ?)"
                  

                  But how can I get this for a DELETE statement?

                  AppUser::where(['id'=>'1'])->delete()->toSql();
                  

                  PHP error: Call to a member function toSql() on integer on line 1

                  This executes the statement, but I would like to get the raw, uninterpolated SQL generated with the question marks standing in for values without actually running the query. The same case holds for any modification statement, such as INSERT or UPDATE

                  But why, who cares?

                  This smells a lot like an xy problem. My web application includes a multi-process architecture. It runs custom artisan commands with asynchronous communication listening in an event-driven loop that update the database.

                  The reason I need the raw query is because I want to reuse a prepared statement for performance efficiency. Unfortunately, the eloquent methods do not expose the prepared statement, so in order to reuse one, I'll have to prepare it myself from the underlying PDO connection.

                  $sql = 'UPDATE `foo` SET `bar` = ? WHERE (`id` = ?)';
                  
                  $statement = DB::connection()->getPdo()->prepare($sql);
                  
                  while (true) {
                      $data = foo();
                      $statement->execute([$data->bar, $data->id]);
                  }
                  

                  However, this departs from the abstracted SQL grammar builder. Because I'm using MySQL at the moment, I can syntactically include the backticks. But now I'm stuck with vendor lock-in. Say for example, the boss says we're moving to MS SQL Server tomorrow, then it's likely going to be annoying (at least) to have to catch bugs for using backticks instead of square braces.

                  I want to use the dynamically generated SQL grammar for reusing in a prepared statement.

                  解决方案

                  First get a query builder instance for the model's table.

                  $builder = DB::table((new User)->getTable());
                  

                  Then get the grammar and compile the delete statement from the builder with a where clause.

                  $sql = $builder->getGrammar()->compileDelete($builder->where('id', 1));
                  

                  "delete from `users` where `id` = ?"
                  

                  Now you can freely swap out database drivers and still get the appropriate platform syntax.

                  这篇关于如何获取 Laravel 删除/更新/插入语句的原始 SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel bigInteger 在关系中四舍五入为 int 下一篇:whereBetween Dates in laravel 4 eloquent

                  相关文章

                  最新文章

                    • <bdo id='mTj2R'></bdo><ul id='mTj2R'></ul>

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

                    <tfoot id='mTj2R'></tfoot>
                    <legend id='mTj2R'><style id='mTj2R'><dir id='mTj2R'><q id='mTj2R'></q></dir></style></legend>

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