1234567891011121314151617181920212223242526272829 |
- <?php
- namespace App\Http\Utils;
- use Illuminate\Support\Facades\DB;
- class BaseUtil
- {
- /**
- * sql监听
- */
- public static function listenSql()
- {
- 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);
- //记录sql
- LoggerFactoryUtil::addSqlMessage($rawSql);
- // echo $rawSql, "\n<br /><br />\n";
- });
- }
- }
|