Laravel prints the executed SQL statement, laravelsql
Open app \ Providers \ AppServiceProvider. PHP and add the following content to the boot method:
Earlier than 5.2
// Introduce DBuse DB first; // or directly use \ DB: listen (function ($ SQL, $ bindings, $ time) {dump ($ SQL );});
Use DB; // or directly use \ DB: // only one parameter QueryExecuted {#84 rows + 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) ;}); // if you want to add the database to the log file:: 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 na Me 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 );});
5.2 and later versions