LoginSignup
1
3

More than 5 years have passed since last update.

SQL クエリログを取得する

Last updated at Posted at 2017-08-25
<?php
use Illuminate\Support\Facades\DB;

// …

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

// SQL 実行を伴う操作

$queries = DB::getQueryLog();

5.0 の頃のドキュメントには載ってたんだけど 5.4 だと見当たらなくて、そのうち見失いそうなのでメモ。5.4 でも tinker で試して動いた。

ひとつの Eloquent Builder に対しては ->toSql() ->getBindings() を使うという方法もある。以下引用。

stackoverflow.com/a/32373344/4821980
$query = User::first()->jobs();

dd($query->toSql(), $query->getBindings());

常にクエリログを出力したいような用途の場合は ServiceProvider 辺りでゴニョしてファイルに吐いたり Debugbar 使ってブラウザで見たりしたほうがいいですよ。

参考

https://laravel.com/docs/5.0/database#query-logging
https://stackoverflow.com/a/32373344/4821980

1
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
1
3