//运行命令 php artisan make:listener QueryListener --event=IlluminateDatabaseEventsQueryExecuted
//创建的事件文件代码如下: <?php namespace AppListeners; use IlluminateDatabaseEventsQueryExecuted; use IlluminateQueueInteractsWithQueue; use IlluminateContractsQueueShouldQueue; class QueryListener { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param QueryExecuted $event * @return void */ public function handle(QueryExecuted $event) { // $sql = str_replace("?", "'%s'", $event->sql); $log = vsprintf($sql, $event->bindings); Log::channel('sqlog')->info($log); } }
在event服务提供者里配置下listen属性:
protected $listen = [ 'AppEventsEvent' => [ 'AppListenersEventListener', ], //指定sql执行监听 'IlluminateDatabaseEventsQueryExecuted' => [ 'AppListenersQueryListener' ] ];