Home > Backend Development > PHP Tutorial > About Laravel5. How to print out the executed sql statement

About Laravel5. How to print out the executed sql statement

不言
Release: 2023-03-31 21:44:02
Original
3005 people have browsed it

This article mainly introduces the method of printing out the executed sql statement in Laravel5.*. It has certain reference value. Interested friends can refer to it.

This article introduces the method of printing out the executed sql statement in Laravel5.* The method of executing the sql statement is shared with everyone. The details are as follows:

Open app\Providers\AppServiceProvider.PHP and add the following content in the boot method

Versions below 5.2

// 先引入DB 
use DB;
// 或者直接使用 \DB::
 DB::listen(function($sql, $bindings, $time) {
        dump($sql);
      });
Copy after login

5.2 and above version

use DB;
// 或者直接使用 \DB::
// 只能接受一个参数

QueryExecuted {#84 ▼
 +sql: "select * from `posts` where `slug` = ? limit 1"
 +bindings: array:1 [▶]
 +time: 0.59
 +connection: MySqlConnection {#85 ▶}
 +connectionName: "mysql"
}

 DB::listen(function($sql) {
        dump($sql);
        // echo $sql->sql;
        // dump($sql->bindings);
      });

// 如果要放入日志文件中
DB::listen(
  function ($sql) {
    // $sql is an object with the properties:
    // sql: The query
    // bindings: the sql query variables
    // time: The execution time for the query
    // connectionName: The name of the connection

    // To save the executed queries to file:
    // Process the sql and the bindings:
    foreach ($sql->bindings as $i => $binding) {
      if ($binding instanceof \DateTime) {
        $sql->bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
      } else {
        if (is_string($binding)) {
          $sql->bindings[$i] = "'$binding'";
        }
      }
    }

    // Insert bindings into query
    $query = str_replace(array('%', '?'), array('%%', '%s'), $sql->sql);

    $query = vsprintf($query, $sql->bindings);

    // Save the query to file
    $logFile = fopen(
      storage_path('logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . '_query.log'),
      'a+'
    );
    fwrite($logFile, date('Y-m-d H:i:s') . ': ' . $query . PHP_EOL);
    fclose($logFile);
  }
);
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Passed Laravel "standard" development of SMS verification code sending function

Learning to transfer array to view in Laravel5 framework

Learning the basic processes of models, controllers and views of the Laravel 5 framework

The above is the detailed content of About Laravel5. How to print out the executed sql statement. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template