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的错。

  1. DB::connection()->enableQueryLog();
  2. $data = DB::table('table')->where(array('xxx'=>'xxx'))->get();
  3. var_dump(DB::getQueryLog());

http://stackoverflow.com/questions/29096853/how-to-print-sql-statement-in-laravel-5/29097087

还有需要注意的地方:如果查询的不是配置里写的默认数据库那么打印也得跟着变化,例如:

  1. DB::connection("site_read")->enableQueryLog();
  2. $data = DB::connection("site_read")->table('b_top_tag')->where(array('pk_date'=>'2015-03-15'))->orderBy('index_num','desc')->take(5)->get();
  3. var_dump($data);
  4. var_dump(DB::connection("site_read")->getQueryLog());

连接指定数据库

  1. DB::connection("site_read")

具体的使用上面的例子已经附带了。

 

这样其实比较麻烦,最后采取的方法是

安装了laravel的debugbar

教程链接

 

 

 

posted @ 2015-09-18 15:56  午时的海  阅读(1477)  评论(0编辑  收藏  举报