Skip to content

QueryExecuted event is not fired when run a query #112

@kevinlau-drewberry

Description

@kevinlau-drewberry

I created the below event listener and bound it to the QueryExecuted event.

<?php
namespace App\Listeners;

use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\Log;

class QueryExecutedListener
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(QueryExecuted $query): void
    {
        $message = [
            'query' => $query->sql,
            'bindings' => $query->bindings,
            'time' => $query->time
        ];
        Log::error(json_encode($message));
    }
}

but no log is saved to the channel after a query ran.
The listener is registered successfully which I can verify with the command php artisan event:list

 Illuminate\Database\Events\QueryExecuted .........................................................................................................
  ⇂ Sentry\Laravel\EventHandler@queryExecuted
  ⇂ Sentry\Laravel\Tracing\EventHandler@queryExecuted
  ⇂ Spatie\LaravelIgnition\Recorders\QueryRecorder\QueryRecorder@record
  ⇂ App\Listeners\QueryExecutedListener@handle

This may because in Illuminate\Database\Connection class function event(), $this->events is always return null.

May I have a pointer how I can set the events properly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions