LoginSignup
7
3

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-11-16

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

$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
  ]
]
7
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3