Laravel监听数据库访问、打印SQL
2016-03-10 17:31
726 查看
增加一个helper函数
在需要开始监听数据库访问的位置添加代码
sql语句会被打印到标准输出。
if ( ! function_exists('sql_dump')) { function sql_dump() { \DB::listen(function ($sql) { $i = 0; $bindings = $sql->bindings; $rawSql = preg_replace_callback('/\?/', function ($matches) use ($bindings, &$i) { $item = isset($bindings[$i]) ? $bindings[$i] : $matches[0]; $i++; return gettype($item) == 'string' ? "'$item'" : $item; }, $sql->sql); echo $rawSql, "\n<br /><br />\n"; }); } }
在需要开始监听数据库访问的位置添加代码
DB::connection()->enableQueryLog(); // 打开query log sql_dump();
sql语句会被打印到标准输出。
相关文章推荐
- 概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM
- phpstrtotime()对于31日求上个月有问题
- HDU 1.1.4 A+B for Input-Output Practice (IV)
- HDU 1.1.3 A+B for Input-Output Practice (III)
- PHP排序函数
- HDU 1.1.2 A+B for Input-Output Practice (II)
- HDU 1.1.1 A+B for Input-Output Practice (I)
- 申请TexturePacker免费注册码(TP:639BBF0B)
- autocomplete+PHP+MYSQL的实现模糊查询
- PHP 魔术变量和魔术函数
- Php笔记-基础语法
- 配置 IIS 下的FTP服务器
- YII 邮件发送功能
- CakePHP 3.2插件引入插件篇
- php curl
- Geoip geoip-api-php 库包使用 - 通过ip 找到国家
- JS PHP 乱入
- Selenium WebDriver get ,getScreenshotAs(OutputType.FILE);
- PHPexcel 判断日期类型
- getOutputStream() has already been called for this response