创建监听器
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); } }