laravel 使用dingo api 添加sql调试

1、在appServiceProvider启动boot中添加

 if(env("APP_DEBUG"))\DB::enableQueryLog();

2、在需要调试的地方直接使用:

dd(\DB::getQueryLog());

 

 

以下采用更加优雅的方式实现(配合debugbar):

前提:需要安装debugbar扩展

1、添加json返回处理的中间件 ProcessJsonResponse

 1 namespace App\Http\Middleware;
 2     use Closure;
 3     use Dingo\Api\Http\Response;
 4 
 5     class ProcessJsonResponse
 6     {
 7         /**
 8          * Handle an incoming request.
 9          *
10          * @param  \Illuminate\Http\Request  $request
11          * @param  \Closure  $next
12          * @return mixed
13          */
14         public function handle($request, Closure $next)
15         {
16             $response = $next($request);
17             if (
18                 env("APP_DEBUG")&&
19                 $request->ajax()&&
20                 $response instanceof Response &&
21                 app()->bound('debugbar') &&
22                 app('debugbar')->isEnabled()
23             ) {
24                 $debugbar_data=app('debugbar')->getData();
25                 $queries_data = $this->sqlFilter($debugbar_data);
26                 $total_duration_time=array_get($debugbar_data, 'time.duration_str');
27                 $response->setContent(json_decode($response->morph()->getContent(), true) + [
28                         '_debugbar' => [
29                             'total_queries' => count($queries_data),
30                             'total_duration_time'=>$total_duration_time,
31                             'queries' => $queries_data,
32                         ]
33                     ]);
34             }
35 
36             return $response;
37         }
38 
39         /**
40          * Get only sql and each duration
41          *
42          * @param $debugbar_data
43          * @return array
44          */
45         protected function sqlFilter($debugbar_data) {
46             $result = array_get($debugbar_data, 'queries.statements');
47 
48             return array_map(function ($item) {
49                 return [
50                     'sql' => array_get($item, 'sql'),
51                     'duration' => array_get($item, 'duration_str'),
52                 ];
53             }, $result);
54         }
55     }

 

2、修改配置文件api.php添加全局中间件控制(也可以参照laravel自行添加路由中间件)

1 'middleware' => [
2         \App\Http\Middleware\ProcessJsonResponse::class,//dingo api 接口返回内容添加sql by winston
3     ],

 效果:

 

转载于:https://www.cnblogs.com/winstonsias/p/10223942.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值