About Laravel5. How to print out the SQL statements executed

Source: Internet
Author: User
This article mainly introduces the laravel5.* print out the execution of the SQL statement method, has a certain reference value, interested in small partners can refer to

This article describes the laravel5.* print out the execution of the SQL statement method, share to everyone, specifically as follows:

Open app\providers\appserviceprovider.php and add the following to the boot method

5.2 The following versions

DB use db;//is introduced first or directly using \db:: Db::listen (Function ($sql, $bindings, $time) {        dump ($sql);      });

Version 5.2 and above

Use db;//or directly using \db:://can only accept one parameter queryexecuted {#84 +sql: "SELECT * from ' posts ' where ' slug ' =? Limit 1 "+bindings:array:1 [] +time:0.59 +connection:mysqlconnection {#85} +connectionname:" MySQL "} db::listen (Functi        On ($sql) {dump ($sql);        Echo $sql->sql;      Dump ($sql->bindings); });//If you want to put in a log file Db::listen (function ($sql) {//$sql is a object with the properties://sql:the query//bin Dings:the SQL query variables//time:the execution time for the query//connectionname:the name of the Connecti  On/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 (' percent ', '%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); });

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.