BaseUtil.php 701 B

1234567891011121314151617181920212223242526272829
  1. <?php
  2. namespace App\Http\Utils;
  3. use Illuminate\Support\Facades\DB;
  4. class BaseUtil
  5. {
  6. /**
  7. * sql监听
  8. */
  9. public static function listenSql()
  10. {
  11. DB::listen(function ($sql) {
  12. $i = 0;
  13. $bindings = $sql->bindings;
  14. $rawSql = preg_replace_callback('/\?/', function ($matches) use ($bindings, &$i) {
  15. $item = isset($bindings[$i]) ? $bindings[$i] : $matches[0];
  16. $i++;
  17. return gettype($item) == 'string' ? "'$item'" : $item;
  18. }, $sql->sql);
  19. //记录sql
  20. LoggerFactoryUtil::addSqlMessage($rawSql);
  21. // echo $rawSql, "\n<br /><br />\n";
  22. });
  23. }
  24. }