Laravel Tutorials

Simplified Stream Response Handling in Laravel

Published
Simplified Stream Response Handling in Laravel image

Working with HTTP streams in Laravel has traditionally involved multiple steps and conversions, especially when dealing with file downloads or transfers. Laravel's new resource() method streamlines this process, reducing complex stream handling to a single method call.

The method is particularly valuable when downloading files from external sources and storing them in your application's storage system, eliminating the need for temporary files or complex stream conversions.

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
 
// Old approach required multiple conversions
$response = Http::get($url);
Storage::put('file.pdf', StreamWrapper::getResource(
$response->toPsrResponse()->getBody()
));
 
// New streamlined approach
Storage::put('file.pdf', Http::get($url)->resource());

Let's explore a practical example of a document processing service:

<?php
 
namespace App\Services;
 
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use App\Exceptions\DocumentProcessingException;
 
class DocumentProcessor
{
public function processRemoteDocument(string $documentUrl, string $reference)
{
try {
$response = Http::timeout(30)->get($documentUrl);
 
if (!$response->successful()) {
throw new DocumentProcessingException(
"Failed to fetch document: {$response->status()}"
);
}
// Store the original document
$originalPath = "documents/{$reference}/original.pdf";
Storage::put($originalPath, $response->resource());
// Create a backup copy
Storage::disk('backup')->writeStream(
$originalPath,
Storage::readStream($originalPath)
);
return [
'reference' => $reference,
'size' => Storage::size($originalPath),
'path' => $originalPath
];
} catch (Exception $e) {
Log::error('Document processing failed', [
'url' => $documentUrl,
'reference' => $reference,
'error' => $e->getMessage()
]);
 
throw $e;
}
}
}

The resource() method simplifies stream handling in Laravel applications, making file operations more straightforward and reducing the complexity of your code.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article