您好,欢迎访问一九零五行业门户网

关于Laravel5.打印出执行的sql语句的方法

本篇文章主要介绍了laravel5.* 打印出执行的sql语句的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文介绍了laravel5.* 打印出执行的sql语句的方法,分享给大家,具体如下:
打开app\providers\appserviceprovider.php,在boot方法中添加如下内容
5.2以下版本
// 先引入db use db;// 或者直接使用 \db:: db::listen(function($sql, $bindings, $time) { dump($sql); });
5.2及以上版本
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); });
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
通过 laravel “规范” 的开发短信验证码发送功能
laravel5框架中向视图传送array的学习
laravel 5框架的模型和控制器以及视图基础流程的学习
以上就是关于laravel5.打印出执行的sql语句的方法的详细内容。
其它类似信息

推荐信息