aceraven777's avatar

When does Laravel dispatch the job?

I want to know at what point do laravel dispatch the job?

$job = ThisIsTheJob::dispatch($user); // Do laravel dispatch the job here?

$job->afterCommit(); // But I have after commit here

I have tested this already and when I wrap this on DB::transaction its dispatching after commit, if it doesn't it dispatches immediately. I'm just wondering how ->afterCommit() works, because what I know is it's already dispatching on this line ThisIsTheJob::dispatch($user), and how do it know if afterCommit is being called on the next line?

0 likes
2 replies
Snapey's avatar

why would you write it this way?

Snapey's avatar

I think its because the job created is a PendingDispatch which uses the __destruct method to know that no more parameters can be chained.


public function __destruct()
    {
        if (! $this->shouldDispatch()) {
            return;
        } elseif ($this->afterResponse) {
            app(Dispatcher::class)->dispatchAfterResponse($this->job);
        } else {
            app(Dispatcher::class)->dispatch($this->job);
        }
    }

Please or to participate in this conversation.