LoginSignup
9
8

More than 5 years have passed since last update.

[Laravel] 実行されたSQLをログに吐き出す

Posted at

実行された SQL をログに吐き出す

  • 実行されている SQL を見たい

Laravel5.1

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        \DB::listen(function ($sql, $bindings, $time) {
            // ログ吐く処理
        });
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
    }
}

Laravel5.2

  • 5.2 で変わった
<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        \DB::listen(function ($query) {
            // $query->sql
            // $query->bindings
            // $query->time

            // ログ吐く処理
        });
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
    }
}

9
8
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
9
8