Http::batch with hooks in Laravel 12.32
- New in Laravel 12.32: Http::batch
- Http::pool vs. Http::batch
- Http::batch in action
- Helper Methods and Properties
- Wrap‑up
New in Laravel 12.32: Http::batch
A new feature by Wendell Adriel has landed in Laravel 12.32; Http::batch, making it easier to send multiple HTTP requests concurrently with lifecycle hooks.
Http::pool vs. Http::batch
Previously, Laravel’s Http::pool allowed concurrent requests but lacked hooks to handle events before, during, and after execution. With Http::batch, you now have:
-
before– runs before any requests -
progress– runs after each successful request -
catch– runs after each failed request -
then– runs if all requests succeed -
finally– runs after all requests finish
Http::batch in action
$responses = Http::batch(fn (Batch $batch) => [ $batch->get('http://localhost/first'), $batch->get('http://localhost/second'),])->before(function (Batch $batch) { // This runs before the first HTTP request is executed.})->progress(function (Batch $batch, int|string $key, Response $response) { // This runs after each successful HTTP request from the Batch.})->catch(function (Batch $batch, int|string $key, Response|RequestException $response) { // This runs after each failed HTTP request from the Batch.})->then(function (Batch $batch, array $results) { // This runs ONLY IF all the HTTP requests from the Batch are successful and the batch is not cancelled.})->finally(function (Batch $batch, array $results) { // This runs after all the HTTP requests from the Batch finish and the batch is not cancelled.})->send();
You cannot add requests after a batch has started.
Helper Methods and Properties
Alongside the hooks, the \Illuminate\Http\Client\Batch class also offers several helpers to inspect and track the batch:
- $batch->totalRequests → total number of requests.
- $batch->pendingRequests → how many are still waiting.
- $batch->failedRequests → how many have failed.
- $batch->processedRequests() → number of processed requests.
- $batch->hasFailures() → returns true if there are failures.
- $batch->finished() → returns true when all requests are done.
Wrap‑up
Http::batch brings a clean, expressive API for orchestrating concurrent HTTP calls. It’s perfect for multi‑service workflows, batch data fetching, and progress tracking.
Check out the PR for more: #56946
Stay Updated.
I'll you email you as soon as new, fresh content is published.