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

richmhouse's avatar

How to Inject Services into Queued Listeners using Laravel 7

I'm using Laravel 7. I would like like to inject services into a event listener. Injecting their interfaces through the constructor works fine, but when I add "implements ShouldQueue" to the class so it runs as a job, it sometimes works, but I mainly get " Target [App\Services\INotificationService] is not instantiable while building [App\Listeners\NewOpportunityMessage].

laravel.com/docs/7.x/container#resolving says "Additionally, you may type-hint dependencies in the handle method of queued jobs". However if I do this I get a "Too few arguments to function" exception.

laravel.com/docs/7.x/events#defining-listeners says "Your event listeners may also type-hint any dependencies they need on their constructors. All event listeners are resolved via the Laravel service container, so dependencies will be injected automatically".

Please advise on the correct way to inject into a queued listener? Should it be through the constructor or should it be through the handle method?

I've removed the start of the above laravel.com links as Laracasts doesn't allow me to include links on my first day.

0 likes
0 replies

Please or to participate in this conversation.