Laravel 5.3 - How to log all queries on a page?

后端 未结 7 1566
生来不讨喜
生来不讨喜 2020-12-24 12:48

My team and I are working on a rather big project. There\'s queries going on everywhere - in controllers, in view composers in views (lazy loading) and probably in some othe

7条回答
  •  暖寄归人
    2020-12-24 13:30

    If you want to print a query which is executed on your app do following steps.

    Step1: Go to your AppServiceProvider.php file. // File path App\Providers\AppServiceProvider.php

    Step2: Make boot() method and paste below code.

    public function boot() {
            // Log queries
            if (true) {
                \DB::listen(function ($query) {
                    \Log::info(
                        $query->sql, $query->bindings, $query->time
                    );
                });
            }
        }
    

    Step3: Now you can see you queries in lumen.log or laravel.log file. File path is laravel_app\storage\logs\laravel.log or lumen.log.

    Enjoy....

提交回复
热议问题