Laravel prints the executed SQL statement, laravelsql

Source: Internet
Author: User

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

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.