AppServiceProvider.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Support\Facades\Schema;
  5. class AppServiceProvider extends ServiceProvider
  6. {
  7. /**
  8. * Register any application services.
  9. *
  10. * @return void
  11. */
  12. public function register()
  13. {
  14. //
  15. if ($this->app->environment() !== 'production') {
  16. $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
  17. }
  18. }
  19. /**
  20. * Bootstrap any application services.
  21. *
  22. * @return void
  23. */
  24. public function boot()
  25. {
  26. //
  27. //Schema::defaultStringLength(191);
  28. app('Dingo\Api\Exception\Handler')->register(function (\Exception $exception) {
  29. $request = \Illuminate\Http\Request::capture();
  30. return app('App\Exceptions\Handler')->render($request, $exception);
  31. });
  32. // 打印所有sql
  33. if(config('app.db_log')) {
  34. \DB::listen(function($query) {
  35. // 先替换sql中的 % 避免 vsprintf 当成参数解析报错 Too few arguments
  36. $tmp = str_replace('%', '(no-vsprintf)', $query->sql);
  37. // \Log::info($tmp);
  38. $tmp = str_replace('?', '"'.'%s'.'"', $tmp);
  39. // \Log::info($tmp);
  40. $tmp = vsprintf($tmp, $query->bindings);
  41. $tmp = str_replace("\\","",$tmp);
  42. // 还原 %
  43. $tmp = str_replace('(no-vsprintf)', '%', $tmp);
  44. \Log::info($tmp);
  45. //Log::info($tmp."\n\n\t");
  46. });
  47. }
  48. }
  49. }