laravel 取sql语句

 \DB::connection()->enableQueryLog();

some sql action...

$query = \DB::getQueryLog();
$lastQuery = end($query); dd($lastQuery);
php artisan tinker

DB::listen(function($sql){ var_dump($sql);  });

Inside AppServiceProvider.php add the following to the boot method:

use DB;
use Event; //.. public function boot()
{
if (env('APP_ENV') === 'local') {
DB::connection()->enableQueryLog();
}
if (env('APP_ENV') === 'local') {
Event::listen('kernel.handled', function ($request, $response) {
if ( $request->has('sql-debug') ) {
$queries = DB::getQueryLog();
dd($queries);
}
});
}
}
上一篇:【洛谷4097】[HEOI2013] Segment(初学李超线段树)


下一篇:慢查询日志工具mysqlsla的使用