Laravel how to get query with bindings?

前端 未结 14 832
小蘑菇
小蘑菇 2021-01-31 15:58

I have some query that I need to pass to another query using query builder

$query = DB::table(\'table\')->whereIn(\'some_field\', [1,2,30])->toSql();

Mode         


        
14条回答
  •  忘了有多久
    2021-01-31 16:46

    Output to the log all queries with inserted bindings sorted from the slowest query to the fastest:

        \DB::enableQueryLog();
    
        // Put here your queries 
        $query = DB::table('table')->whereIn('some_field', [1,2,30]); 
        $query2 = DB::table('table2')->where('some_field', '=', 10); 
    
    
        $logQueries = \DB::getQueryLog();
        usort($logQueries, function($a, $b) {
            return $b['time'] <=> $a['time'];
        });
    
        foreach ($logQueries as $item) {
            \Log::info(str_replace_array('?', $item['bindings'], $item['query']));
            \Log::info($item['time']. ' ms');
        }
    

提交回复
热议问题