打开app\Providers\AppServiceProvider.PHP,在boot方法中添加如下内容

5.2以下版本

// 先引入DB
use DB;
// 或者直接使用 \DB::
DB::listen(function($sql, $bindings, $time) {
dump($sql);
});
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);
}
);

  

5.2及以上版本

最新文章

  1. notepad++快捷键大全
  2. Maven如何传递系统属性变量到TestNG
  3. Android ANR分析
  4. Ajax 的缺点
  5. SqlServer 连接字符串多种配置
  6. 验证(Javascript和正则表达式)
  7. TopCoder SRM 605 DIV1
  8. DHTML【11】--DOM
  9. SwingBench---ORACLE压力测试工具
  10. entity framework core 支持批量插入,值得期待
  11. c#动态编译并执行字符串
  12. 【Qt编程】Qt学习之Window and Dialog Widgets
  13. 【转】H.264中的NAL技术
  14. 运用BT在centos下搭建一个博客论坛
  15. 【RL-TCPnet网络教程】第21章 RL-TCPnet之高效的事件触发框架
  16. Kaldi nnet3的前向计算
  17. 微信小程序:首页设置方法(开发模式,使用模式)与其他相关设置
  18. 插入排序之Java实现
  19. 第65节:Java后端的学习之Spring基础
  20. java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader

热门文章

  1. (转) Linux命令学习手册-arp命令
  2. Nodejs mysql pool使用实例
  3. 打印流-PrintStream
  4. Java的compare比较
  5. 使用Quartz任务调用的时候报错Based on configured schedule, the given trigger will never fire.
  6. redis---安全设置
  7. 构建第一个Spring Boot2.0应用之集成dubbo上---环境搭建(九)
  8. [转]Tomcat日志详解
  9. Cocos2d-x v3.1 Hello world程序(四)
  10. codeforces 599D Spongebob and Squares