Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Danaq's avatar
Level 1

auto event-register if listener has ShoulQueue

Hi,

I'm currently working with Laravel 11 and encountered a behavior that seems unusual to me.

I have two events, each with their respective listeners. These pairs are registered in a separate EventServiceProvider, following the documentation’s guidelines. Additionally, the shouldDiscoverEvents method in this provider is explicitly set to false.

Since these events are provided by a package, I wanted more control over how they are connected to the main application. Here's my EventServiceProvider:

class EventServiceProvider extends ServiceProvider
{
	/**
	 * Register any events for your application.
	 */
	public function boot(): void
	{
		Event::listen(
			DeleteTriggerReceived::class,
			ProcessDeleteTrigger::class,
		);
		Event::listen(
			UpdateTriggerReceived::class,
			ProcessUpdateTrigger::class,
		);
	}
	/**
	 * Determine if events and listeners should be automatically discovered.
	 */
	public function shouldDiscoverEvents(): bool
	{
		return false;
	}
}

Both listeners implement ShouldQueue, as outlined in the documentation.

However, when I run php artisan event:list, I get the following output:

  Censhare\Events\DeleteTriggerReceived ................................................................
  ⇂ App\Listeners\ProcessDeleteTrigger (ShouldQueue)
  ⇂ App\Listeners\ProcessDeleteTrigger@handle (ShouldQueue)
  Censhare\Events\UpdateTriggerReceived ................................................................
  ⇂ App\Listeners\ProcessUpdateTrigger (ShouldQueue)
  ⇂ App\Listeners\ProcessUpdateTrigger@handle (ShouldQueue)

This is not the expected behavior. Instead of a single job being dispatched, two jobs are created in the database. This leads to interference between the jobs when they are processed, as they share data.

Interestingly, when I remove the manual event registration from the service provider everything works as expected—only one job is dispatched to the database. Note, that "shouldDiscoverEvents" is still set to false.

Does anyone know why this is happening? Is this the expected behavior in Laravel 11?

Thanks in advance for any insights

0 likes
1 reply
Danaq's avatar
Danaq
OP
Best Answer
Level 1

This is why one write to a form. As soon as the question is posted, you'll find the answer by yourself ;)

First an foremost: The Problem does not apply only to listeners with ShouldQueue but to all listeners.

The approach to disable the event discovery was wrong. As you can see, the EventServiceProvider inherits from the one the framwork provides. (Illuminate\Support\ServiceProvider)

The framwork-EventServiceProvider does have the shouldDiscoverEvents-method as well as a disableEventDiscovery-method.

If this second one is called before any event registration in the boot-method, only the manual registration is used an therefor only on listener is registered as well as only one job is dispatched.

class EventServiceProvider extends ServiceProvider
{
	/**
	 * Register any events for your application.
	 */
	public function boot(): void
	{
        $this->disableEventDiscovery();

		Event::listen(
			DeleteTriggerReceived::class,
			ProcessDeleteTrigger::class,
		);
		Event::listen(
			UpdateTriggerReceived::class,
			ProcessUpdateTrigger::class,
		);
	}
}
1 like

Please or to participate in this conversation.