Laravl5.6.* 不用寫sql監聽方法,有存在的方法~

        \DB::enableQueryLog();

        $s_date = strtotime(date("Y-m-d")."00:00:00");
        $e_date = strtotime(date("Y-m-d")."23:59:59");

        $SubmitTask = \DB::table('task_submit')->select(['id','stauts'])->where('created_at' , '>=' , $s_date)->where('created_at' , '<=' , $e_date);


        // 1.今日已完成領任務
        $data['finish']  = $SubmitTask->where('status' , 'Y')->count('id');
            
        // 2.今日已領任務
        $data['receive'] = $SubmitTask->where('status' , 'P')->count('id');
        
        // 3.今日待審覈任務
        $data['waiting'] = $SubmitTask->where('status' , 'W')->count('id');

       $info = \DB::getQueryLog();
        
       \Log::info(\DB::getQueryLog());

        return $info;

一篇小案例~

很好用的方法~

今天閒來無事,補充下這個地方

查詢出你的應用裏比較慢的sql語句,只要是頁面上有的,都可以查詢出來

在\app\Providers\AppServiceProvider.php裏面,boot方法裏寫:

DB::listen (function($query)
{
            $sql = $query->sql;
            $bingings = $query->bindings;          
               $time = $query->time;
            Log::debug(compact('sql','bingings','time'));
});



2.在log裏看就可以了,也可以加個條件甚至超過一些時間的時候纔打印這條sql

   

DB::listen (function($query){
            $sql = $query->sql;
            $bingings = $query->bindings;
            $time = $query->time;
            if($time>10){
            Log::debug(compact('sql','bingings','time'));
            }
   });

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章