在src/Illuminate/Database/Connection.php
里打印SQL默认是关闭的,见https://github.com/laravel/framework/commit/e0abfe5c49d225567cb4dfd56df9ef05cc297448
首先需要导入DB
类(use IlluminateSupportFacadesDB;
),否则会报类似于Class 'AppModelsDB' 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