LoginSignup
19
15

More than 5 years have passed since last update.

laravelのSQLのログの出し方

Posted at

laravelで実行されたSQLを確認しようとする際に、bindされた変数を置換するのが面倒なのでそのまま実行可能なSQLをログに吐き出す方法。

app/Providers/AppServiceProvider.php
    public function boot()
    {
        \DB::listen(function ($query) {
            $sql = $query->sql;
            for ($i = 0; $i < count($query->bindings); $i++) {
                $sql = preg_replace("/\?/", $query->bindings[$i], $sql, 1);
            }
            \Log::info($sql);
        });
    }
19
15
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
19
15