laravel中如何打印sql

你也是一片 在我胸口颤动的小叶子。 生命之风将你吹送至此。

第一种方法

1
2
3
4
//in our case,
DB::table('users')->toSql();
//return
select * from users

第二种方法

1
2
3
4
5
6
7
8
9
10
Route::get('haha',function (){
Event::listen('illuminate.query', function($query, $params, $time, $conn)
{
dd(array($query, $params, $time, $conn));
});

\RainLab\User\Models\User::whereBetween('created_at',
['2016-05-26 00:00:00', '2016-06-27 00:00:00']
)->get(['email']);
});

第三种方法

1
2
3
DB::enableQueryLog();
$users = \RainLab\User\Models\User::whereBetween('created_at', ['2016-05-26 00:00:00', '2016-06-27 00:00:00'])->get();
dd(DB::getQueryLog());

第四种方法

1
2
3
4
5
6
7
8
9
10
11
function trace_sql($dump = false)
{
\DB::listen(function ($event) use($dump) {
if ($dump) {
dump($event->sql);
dump($event->bindings);
}
info($event->sql);
info($event->bindings);
});
}

最后一种方法

1
2
//安装 debug
composer require barryvdh/laravel-debugbar