Skip to content

[13.x] Add dispatchSyncIf and dispatchSyncUnless to Dispatchable#59736

Closed
bipinks wants to merge 1 commit intolaravel:13.xfrom
bipinks:feature/dispatch-sync-if
Closed

[13.x] Add dispatchSyncIf and dispatchSyncUnless to Dispatchable#59736
bipinks wants to merge 1 commit intolaravel:13.xfrom
bipinks:feature/dispatch-sync-if

Conversation

@bipinks
Copy link
Copy Markdown
Contributor

@bipinks bipinks commented Apr 17, 2026

Adds dispatchSyncIf and dispatchSyncUnless to complete the conditional-dispatch API on the Dispatchable trait.

Motivation

Dispatchable already has conditional variants for the queued dispatch path:

Job::dispatchIf($boolean, ...$arguments);
Job::dispatchUnless($boolean, ...$arguments);

…but the synchronous dispatch path has no matching variants. Users who want to conditionally run a job in the current process are forced to an explicit if statement, and the naming of dispatchIf actively invites a subtle bug:

// LOOKS like conditional sync dispatch. IS NOT — this queues the job.
Job::dispatchIf($shouldRunNow, $order);

There is no chainable fallback either — PendingDispatch has no ->sync() method — so the conditional-sync case has no fluent equivalent today.

New methods

Method Behavior
dispatchSyncIf($boolean, ...$args) Dispatches synchronously when truthy; returns Fluent no-op otherwise
dispatchSyncUnless($boolean, ...$args) Dispatches synchronously when falsy; returns Fluent no-op otherwise

Both accept a bool or Closure, matching the exact shape of dispatchIf / dispatchUnless.

Usage

// Before
if ($shouldSync) {
    ProcessPayment::dispatchSync($order);
}

// After
ProcessPayment::dispatchSyncIf($shouldSync, $order);
ProcessPayment::dispatchSyncUnless($queued, $order);

Tests

Added 4 new integration tests in tests/Integration/Queue/JobDispatchingTest.php mirroring the existing coverage for dispatchIf / dispatchUnless: boolean + Closure cases for both methods. All 13 tests in the file and 79 tests/Bus/ tests pass.

No breaking changes

New methods only; existing behavior untouched.

@taylorotwell
Copy link
Copy Markdown
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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

Successfully merging this pull request may close these issues.

2 participants