title: How to get the raw SQL for a Laravel delete/update/insert statement?
body: <p>I know I can use the <code>toSql</code> method on a query builder to get the raw SQL with binding parameter placeholders for a <code>SELECT</code> statement.</p>
  
  <pre><code>App\User::where(['id'=&gt;'1'])-&gt;toSql();
  </code></pre>
  
  <blockquote>
  <pre><code>"select * from `users` where (`id` = ?)"
  </code></pre>
  </blockquote>
  
  <p>But how can I get this for a <code>DELETE</code> statement?</p>
  
  <pre><code>App\User::where(['id'=&gt;'1'])-&gt;delete()-&gt;toSql();
  </code></pre>
  
  <blockquote>
    <p>PHP error:  Call to a member function toSql() on integer on line 1</p>
  </blockquote>
  
  <p>This executes the statement, but I would like to get the raw, uninterpolated SQL generated with the question marks standing in for values <em>without</em> actually running the query. The same case holds for any modification statement, such as <code>INSERT</code> or <code>UPDATE</code></p>
  
  <h2>But why, who cares?</h2>
  
  <p>This smells a lot like an <a href="https://meta.stackexchange.com/a/66378/321521">xy problem</a>. 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.</p>
  
  <p>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.</p>
  
  <pre><code>$sql = 'UPDATE `foo` SET `bar` = ? WHERE (`id` = ?)';
  
  $statement = DB::connection()-&gt;getPdo()-&gt;prepare($sql);
  
  while (true) {
      $data = foo();
      $statement-&gt;execute([$data-&gt;bar, $data-&gt;id]);
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>I want to use the dynamically generated SQL grammar for reusing in a prepared statement.</p>