Laravel 输入SQL语句到日志

在AppServiceProvider.php中的boot方法中添加如下代码
DB::listen(function ($query) {
        $tmp = str_replace('?', '"'.'%s'.'"', $query->sql);
        $qBindings = [];
        foreach ($query->bindings as $key => $value) {
                if (is_numeric($key)) {
                        $qBindings[] = $value;
                } else {
                        $tmp = str_replace(':'.$key, '"'.$value.'"', $tmp);
                }
        }
        $tmp = vsprintf($tmp, $qBindings);
        $tmp = str_replace("\\", "", $tmp);
        \Log::info(' execution time: '.$query->time.'ms; '.$tmp."\n\n\t");
});

你可能感兴趣的:(Laravel 输入SQL语句到日志)