From 02b3fdd76d7e33cd4de92fa432bce2603a8ea8c6 Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Tue, 14 Oct 2025 13:32:43 +0100 Subject: [PATCH 1/3] Add defer documentation to HTTP batch --- http-client.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/http-client.md b/http-client.md index ba3a8b50a3..418dcdffb8 100644 --- a/http-client.md +++ b/http-client.md @@ -621,6 +621,33 @@ $batch->finished(); // Indicates if the batch has request failures... $batch->hasFailures(); ``` + +#### Deferring Batches + +If you are not interested in the results returned by requests in the batch, you should consider using the `defer` method. When the `defer` method is invoked, the batch is not executed immediately. Instead, Laravel will execute the batch after the HTTP response has been sent to the user: + +```php +use Illuminate\Http\Client\Batch; +use Illuminate\Http\Client\RequestException; +use Illuminate\Http\Client\Response; +use Illuminate\Support\Facades\Http; + +$responses = Http::batch(fn (Batch $batch) => [ + $batch->get('http://localhost/first'), + $batch->get('http://localhost/second'), + $batch->get('http://localhost/third'), +])->before(function (Batch $batch) { + // The batch has been created but no requests have been initialized... +})->progress(function (Batch $batch, int|string $key, Response $response) { + // An individual request has completed successfully... +})->then(function (Batch $batch, array $results) { + // All requests completed successfully... +})->catch(function (Batch $batch, int|string $key, Response|RequestException $response) { + // First batch request failure detected... +})->finally(function (Batch $batch, array $results) { + // The batch has finished executing... +})->defer(); +``` ## Macros From 9307bd70659e9412a746a7269bdb2f0178862755 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 14 Oct 2025 08:47:36 -0500 Subject: [PATCH 2/3] Update http-client.md --- http-client.md | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/http-client.md b/http-client.md index 418dcdffb8..5c23c05d7f 100644 --- a/http-client.md +++ b/http-client.md @@ -624,7 +624,7 @@ $batch->hasFailures(); #### Deferring Batches -If you are not interested in the results returned by requests in the batch, you should consider using the `defer` method. When the `defer` method is invoked, the batch is not executed immediately. Instead, Laravel will execute the batch after the HTTP response has been sent to the user: +When the `defer` method is invoked, the batch of requests is not executed immediately. Instead, Laravel will execute the batch after the current application request's HTTP response has been sent to the user, keeping your application feeling fast and responsive: ```php use Illuminate\Http\Client\Batch; @@ -636,16 +636,8 @@ $responses = Http::batch(fn (Batch $batch) => [ $batch->get('http://localhost/first'), $batch->get('http://localhost/second'), $batch->get('http://localhost/third'), -])->before(function (Batch $batch) { - // The batch has been created but no requests have been initialized... -})->progress(function (Batch $batch, int|string $key, Response $response) { - // An individual request has completed successfully... -})->then(function (Batch $batch, array $results) { +])->then(function (Batch $batch, array $results) { // All requests completed successfully... -})->catch(function (Batch $batch, int|string $key, Response|RequestException $response) { - // First batch request failure detected... -})->finally(function (Batch $batch, array $results) { - // The batch has finished executing... })->defer(); ``` From 4775bc87d3684721492abc70561aed99540415e0 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 14 Oct 2025 08:47:58 -0500 Subject: [PATCH 3/3] Update http-client.md --- http-client.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/http-client.md b/http-client.md index 5c23c05d7f..53dc9b0811 100644 --- a/http-client.md +++ b/http-client.md @@ -628,8 +628,6 @@ When the `defer` method is invoked, the batch of requests is not executed immedi ```php use Illuminate\Http\Client\Batch; -use Illuminate\Http\Client\RequestException; -use Illuminate\Http\Client\Response; use Illuminate\Support\Facades\Http; $responses = Http::batch(fn (Batch $batch) => [