laravel

[Laravel]直近のクエリ文を出力する

More than 1 year has passed since last update.


クエリ文を文字列として出力する

$sql = User::orderBy('id', 'desc')->toSql();

var_dump($sql);


  • 出力結果

 'select * from `users` where `users`.`deleted_at` is null order by `id` desc' (length=75)


もっと詳細を出力する場合

DB::enableQueryLog();

$users = User::orderBy('id', 'desc')->paginate(30);
dd(DB::getQueryLog());


  • 出力結果

array:2 [
0 => array:3 [
"query" => "select count(*) as aggregate from `users` where `users`.`deleted_at` is null"
"bindings" => []
"time" => 0.38
]
1 => array:3 [
"query" => "select * from `users` where `users`.`deleted_at` is null order by `id` desc limit 20 offset 0"
"bindings" => []
"time" => 0.56
]
]