[13.x] Allow Dependency Injection in the failed() method of queued jobs#59566
Closed
michalwolinski wants to merge 3 commits intolaravel:13.xfrom
Closed
[13.x] Allow Dependency Injection in the failed() method of queued jobs#59566michalwolinski wants to merge 3 commits intolaravel:13.xfrom
michalwolinski wants to merge 3 commits intolaravel:13.xfrom
Conversation
59ffb0c to
3176405
Compare
rodrigopedra
reviewed
Apr 6, 2026
|
|
||
| if (method_exists($command, 'failed')) { | ||
| $command->failed($e); | ||
| $this->container->call([$command, 'failed'], ['e' => $e, 'exception' => $e]); |
Contributor
There was a problem hiding this comment.
$this->container->call([$command, 'failed'], [\Throwable::class => $e]);We can provide class names as keys to the Container@call method, so we don't need to guess the parameter's name.
Author
There was a problem hiding this comment.
@rodrigopedra Thank you for the suggestion! Updated in 4f33f24 — using Throwable::class as the key is indeed cleaner and avoids guessing the parameter name.
Author
|
Hey @taylorotwell, thanks for looking. Just curious, is DI in the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently, the
failed()method on queued jobs does not support Dependency Injection. When a job fails, developers are forced to resolve services manually through the service container, which is difficult to test and inconsistent with the rest of the framework:This PR enables type-hinting dependencies directly in
failed(), consistent with howhandle()already works:Benefit to end users
Developers can now inject any service registered in the container into
failed(), making failure-handling logic cleaner, more expressive, and fully testable without relying onapp()helpers.Backwards compatibility
The change is fully backwards compatible. Existing
failed(Throwable $e)methods continue to work without modification — the exception is resolved by its type, consistent with how the container resolves other dependencies. Any additional parameters are injected from the container automatically, identical to howhandle()behaves.Tests
The tests verify both the new approach to dependency injection and legacy backward-compatibility scenarios.