Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QueryExecuted event is not fired when run a query #112

Open
kevinlau-drewberry opened this issue May 21, 2024 · 1 comment
Open

QueryExecuted event is not fired when run a query #112

kevinlau-drewberry opened this issue May 21, 2024 · 1 comment

Comments

@kevinlau-drewberry
Copy link

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?

@kevinlau-drewberry
Copy link
Author

reference on laravel queryExecuted event https://laravel.com/docs/10.x/database#listening-for-query-events

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant