Laravel5 打印SQL
在src/Illuminate/Database/Connection.php
里打印SQL默认是关闭的,见https://github.com/laravel/framework/commit/e0abfe5c49d225567cb4dfd56df9ef05cc297448
首先需要导入DB
类(use Illuminate\Support\Facades\DB;
),否则会报类似于Class 'App\Models\DB' not found
的错。
- DB::connection()->enableQueryLog();
- $data = DB::table('table')->where(array('xxx'=>'xxx'))->get();
- var_dump(DB::getQueryLog());
http://stackoverflow.com/questions/29096853/how-to-print-sql-statement-in-laravel-5/29097087
还有需要注意的地方:如果查询的不是配置里写的默认数据库那么打印也得跟着变化,例如:
- DB::connection("site_read")->enableQueryLog();
- $data = DB::connection("site_read")->table('b_top_tag')->where(array('pk_date'=>'2015-03-15'))->orderBy('index_num','desc')->take(5)->get();
- var_dump($data);
- var_dump(DB::connection("site_read")->getQueryLog());
连接指定数据库
- DB::connection("site_read")
具体的使用上面的例子已经附带了。
这样其实比较麻烦,最后采取的方法是
安装了laravel的debugbar
作者:Wicub
免责声明:文章、笔记等仅供分享、探讨、参考等学习之用,因此造成的任何后果概不负责。(如有错误、疏忽等问题,欢迎指正、讨论,谢谢)
本文版权归作者和博客园共有,欢迎转载,但请务必在文章页面明显位置给出原文连接,谢谢配合。