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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 3, 2025

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

      June 3, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 3, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 3, 2025

      SteelSeries reveals new Arctis Nova 3 Wireless headset series for Xbox, PlayStation, Nintendo Switch, and PC

      June 3, 2025

      The Witcher 4 looks absolutely amazing in UE5 technical presentation at State of Unreal 2025

      June 3, 2025

      Razer’s having another go at making it so you never have to charge your wireless gaming mouse, and this time it might have nailed it

      June 3, 2025

      Alienware’s rumored laptop could be the first to feature NVIDIA’s revolutionary Arm-based APU

      June 3, 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

      easy-live2d – About Make your Live2D as easy to control as a pixi sprite! Live2D Web SDK based on Pixi.js.

      June 3, 2025
      Recent

      easy-live2d – About Make your Live2D as easy to control as a pixi sprite! Live2D Web SDK based on Pixi.js.

      June 3, 2025

      From Kitchen To Conversion

      June 3, 2025

      Perficient Included in Forrester’s AI Technical Services Landscape, Q2 2025

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

      SteelSeries reveals new Arctis Nova 3 Wireless headset series for Xbox, PlayStation, Nintendo Switch, and PC

      June 3, 2025
      Recent

      SteelSeries reveals new Arctis Nova 3 Wireless headset series for Xbox, PlayStation, Nintendo Switch, and PC

      June 3, 2025

      The Witcher 4 looks absolutely amazing in UE5 technical presentation at State of Unreal 2025

      June 3, 2025

      Razer’s having another go at making it so you never have to charge your wireless gaming mouse, and this time it might have nailed it

      June 3, 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.

    Hostinger

    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

    Development

    The Open Source LLM Agent Handbook: How to Automate Complex Tasks with LangGraph and CrewAI

    June 3, 2025
    Artificial Intelligence

    Markus Buehler receives 2025 Washington Award

    June 3, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    CVE-2025-46726 – Langroid XMLToolMessage XML External Entity (XXE) Denial of Service (DoS) and Local File Information Exposure

    Common Vulnerabilities and Exposures (CVEs)

    How to Update the PC Health Check App

    Development

    CVE-2025-48748 – Netwrix Directory Manager Hard-Coded Password Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4018 – Novel-Plus Remote Authentication Bypass

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    Development

    Part 2: Sitecore Quick Guide for the Beginner

    January 3, 2025

    In the previous blog, Part 1: Sitecore Quick Guide for the Beginner, we covered essential…

    Generating Gender Alternatives in Machine Translation

    August 8, 2024

    Bevy – data-driven game engine

    February 16, 2025

    How to Set Up Social Authentication with Strapi and Nuxt.js

    January 14, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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