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!