Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 5, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 5, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 5, 2025

      CodeSOD: Integral to a Database Read

      June 5, 2025

      Players aren’t buying Call of Duty’s “error” excuse for the ads Activision started forcing into the game’s menus recently

      June 4, 2025

      In Sam Altman’s world, the perfect AI would be “a very tiny model with superhuman reasoning capabilities” for any context

      June 4, 2025

      Sam Altman’s ouster from OpenAI was so dramatic that it’s apparently becoming a movie — Will we finally get the full story?

      June 4, 2025

      One of Microsoft’s biggest hardware partners joins its “bold strategy, Cotton” moment over upgrading to Windows 11, suggesting everyone just buys a Copilot+ PC

      June 4, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Enable Flexible Pattern Matching with Laravel’s Case-Insensitive Str::is Method

      June 5, 2025
      Recent

      Enable Flexible Pattern Matching with Laravel’s Case-Insensitive Str::is Method

      June 5, 2025

      Laravel OpenRouter

      June 5, 2025

      This Week in Laravel: Starter Kits, Alpine, PDFs and Roles/Permissions

      June 5, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      FOSS Weekly #25.23: Helwan Linux, Quarkdown, Konsole Tweaks, Keyboard Shortcuts and More Linux Stuff

      June 5, 2025
      Recent

      FOSS Weekly #25.23: Helwan Linux, Quarkdown, Konsole Tweaks, Keyboard Shortcuts and More Linux Stuff

      June 5, 2025

      Grow is a declarative website generator

      June 5, 2025

      Raspberry Pi 5 Desktop Mini PC: Benchmarking

      June 5, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Optimizing Large Data Delivery with Laravel Streaming Responses

    Optimizing Large Data Delivery with Laravel Streaming Responses

    January 7, 2025

    Optimizing Large Data Delivery with Laravel Streaming Responses

    Laravel’s streaming response feature enables efficient handling of large datasets by sending data incrementally as it’s generated, reducing memory usage and improving response times.

    Route::get('/stream', function () {
        return response()->stream(function () {
            foreach (range(1, 100) as $number) {
                echo "Line {$number}n";
                ob_flush();
                flush();
            }
        }, 200, ['Content-Type' => 'text/plain']);
    });
    

    Let’s explore a practical example of streaming a large data export:

    <?php
    
    namespace AppHttpControllers;
    
    use AppModelsOrder;
    use IlluminateSupportFacadesDB;
    
    class ExportController extends Controller
    {
        public function exportOrders()
        {
            return response()->stream(function () {
                // Output CSV headers
                echo "Order ID,Customer,Total,Status,Daten";
                ob_flush();
                flush();
                
    // Process orders in chunks to maintain memory efficiency
                Order::query()
                    ->with('customer')
                    ->orderBy('created_at', 'desc')
                    ->chunk(500, function ($orders) {
                        foreach ($orders as $order) {
                            echo sprintf(
                                "%s,%s,%.2f,%s,%sn",
                                $order->id,
                                str_replace(',', ' ', $order->customer->name),
                                $order->total,
                                $order->status,
                                $order->created_at->format('Y-m-d H:i:s')
                            );
                            
                            ob_flush();
                            flush();
                        }
                    });
            }, 200, [
                'Content-Type' => 'text/csv',
                'Content-Disposition' => 'attachment; filename="orders.csv"',
                'X-Accel-Buffering' => 'no'
            ]);
        }
    }
    

    Streaming responses enable efficient handling of large datasets while maintaining low memory usage and providing immediate feedback to users.


    The post Optimizing Large Data Delivery with Laravel Streaming Responses appeared first on Laravel News.

    Join the Laravel Newsletter to get all the latest
    Laravel articles like this directly in your inbox.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleDummy – Generate PHP class instances populated with dummy data using Faker
    Next Article New Eloquent Relation Existence Methods in Laravel 11.37

    Related Posts

    Security

    ️ Inside the 160-Comment Fight to Fix SnakeYAML’s RCE Default

    June 5, 2025
    Security

    CVE-2025-20286 Credential Reuse Vulnerability in Cisco ISE

    June 5, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    My favorite game of 2024 will be the first physical edition I’ve bought in years — because the freebie is AMAZING

    Development

    Urgent: CISA Flags Cisco Device Risks, Weak Passwords a Major Threat

    Development

    Mysterious Windows 11 “actions” menu appears in latest preview build — here’s what it’s for

    News & Updates

    CVE-2024-6029 – Tesla Model S Iris Modem Firewall Bypass Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    Acronym Decoder – explains acronyms and abbreviations

    January 30, 2025

    Acronym Decoder is a simple tool for quick searching of meaning for various acronyms and…

    CVE-2025-4313 – SourceCodester Advanced Web Store SQL Injection Vulnerability

    May 6, 2025

    You can now wrap your Xbox, Switch, or Steam Deck in glow-in-the-dark dbrand skins — is Surface Pro 12-inch next?

    May 10, 2025

    vdirsyncer – synchronizing calendars and addressbooks

    June 30, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.