Laravel 12.32 Introduces Http::batch() — Parallel Requests, Simplified

Laravel just made HTTP client workflows a whole lot smarter. With the release of Laravel 12.32, we now have access to Http::batch()—a powerful new method that lets you send multiple HTTP requests in parallel, track their progress, and handle success/failure callbacks with elegance.

Whether you’re building a SaaS dashboard, syncing APIs, or fetching data from multiple endpoints, Http::batch() is a DX upgrade you’ll want to use immediately.


🚀 What Is Http::batch()?

Http::batch() is a new method added to Laravel’s HTTP client that allows you to group multiple requests and send them concurrently. It’s built on top of Laravel’s fluent HTTP client and provides a clean, expressive API for managing parallel requests.

✅ Key Features:

  • Add multiple requests with keys
  • Run all requests in parallel
  • Track progress, failures, and completion
  • Chain callbacks like before(), progress(), catch(), then(), and finally()

🧪 Basic Usage

Here’s how you can use it:

use Illuminate\Support\Facades\Http;

$responseBatch = Http::batch()
    ->as('github')->get('https://api.github.com/users/sadique')
    ->as('laravel')->get('https://laravel.com')
    ->before(fn () => logger('Starting batch...'))
    ->progress(fn ($key, $response) => logger("Success: {$key}"))
    ->catch(fn ($key, $response) => logger("Failed: {$key}"))
    ->then(fn () => logger('All requests succeeded!'))
    ->finally(fn () => logger('Batch finished.'))
    ->send();

🔍 Accessing Responses:

$responseBatch['github']->json();
$responseBatch['laravel']->status();

Each request is keyed, making it easy to access individual responses.


🧠 Why It Matters

BenefitImpact for Devs & SaaS Builders
⚡ Parallel ExecutionFaster data fetching from multiple APIs
🧼 Cleaner SyntaxNo need for manual Guzzle pools or async hacks
🔄 Retry LogicEasily handle failures with catch() and finally()
📊 Progress TrackingLog or visualize request progress in real time
🔐 Better UXReduce wait times for dashboards and data-heavy views

🔧 Advanced Use Case: API Syncing in SaaS

Imagine you’re building a Laravel-based SaaS tool that pulls data from GitHub, Stripe, and your own analytics API. With Http::batch(), you can fetch all three simultaneously:

Http::batch()
    ->as('github')->get('https://api.github.com/repos/your-repo')
    ->as('stripe')->withToken($token)->get('https://api.stripe.com/v1/customers')
    ->as('analytics')->get('https://yourapp.com/api/metrics')
    ->progress(fn ($key, $response) => cache()->put("sync_{$key}", $response->json()))
    ->catch(fn ($key, $response) => report("Sync failed for {$key}"))
    ->finally(fn () => event(new SyncCompleted()))
    ->send();

This makes your sync logic cleaner, faster, and easier to maintain.


🧵 Behind the Scenes

The Http::batch() method uses Laravel’s Illuminate\Http\Client\Batch class under the hood. It tracks:

  • Total requests
  • Pending requests
  • Failed requests
  • Completion status

You can even inspect the batch state:

$batch->hasFailures(); // true/false
$batch->processedRequests(); // count
$batch->finished(); // true/false

💡 Developer Tips

  • Use as() to key requests for easy access
  • Chain before() to log or prep before sending
  • Use progress() for real-time feedback (great for Livewire or Inertia dashboards)
  • Handle errors gracefully with catch() and finally()
  • Combine with Laravel Queues for async batch jobs

🔮 Final Thoughts

Laravel 12.32’s Http::batch() is more than syntactic sugar—it’s a performance and DX upgrade. For devs building modern web apps, especially SaaS platforms that rely on external APIs, this feature simplifies complexity and boosts speed.

If you’re already using Laravel’s HTTP client, this is the missing piece you didn’t know you needed.

Leave a Reply

Your email address will not be published. Required fields are marked *