Laravel 实时监听打印 SQL

创建监听器

php artisan make:listener QueryListener --event=Illuminate\Database\Events\QueryExecuted

打开 app/Providers/EventServiceProvider.php ,在 $listen 中添加

protected $listen = [
    'IlluminateDatabaseEventsQueryExecuted' => [
        'AppListenersQueryListener,
    ]
];

  打开 QueryListener 文件

use Log;
public function handle (QueryExecuted $event)
{
    if (env('APP_ENV', 'production') == 'local') {
        $sql = str_replace("?", "'%s'", $event->sql);
        $log = vsprintf($sql, $event->bindings);
        Log::info($log);
    }
}

  

原文地址:https://www.cnblogs.com/gentlemanwuyu/p/11166139.html