Laravel - Queues - Dispatching After The Response Is Sent To Browser

Alternatively, the dispatchAfterResponse method delays dispatching a job until after the HTTP response is sent to the user's browser. This will still allow the user to begin using the application even though a queued job is still executing. This should typically only be used for jobs that take about a second, such as sending an email. Since they are processed within the current HTTP request, jobs dispatched in this fashion do not require a queue worker to be running in order for them to be processed:

    
    use App\Jobs\SendNotification;
    
    SendNotification::dispatchAfterResponse();
	

You may also dispatch a closure and chain the afterResponse method onto the dispatch helper to execute a closure after the HTTP response has been sent to the browser:

    
    use App\Mail\WelcomeMessage;
    use Illuminate\Support\Facades\Mail;
    
    dispatch(function () {
        Mail::to('[email protected]')->send(new WelcomeMessage);
    })->afterResponse();