Home API Tools Posts Hire Me About
Sign In Create Account
Livewire

Building Export to CSV with Livewire v4

February 01, 2026 1 min read 15 views

Export data to downloadable CSV.

Component

@php
new class extends Livewire\Component {
    public function export()
    {
        $filename = 'export-' . now()->format('Y-m-d') . '.csv';
        
        return response()->streamDownload(function () {
            $handle = fopen('php://output', 'w');
            fputcsv($handle, ['Name', 'Email', 'Created']);
            
            User::chunk(100, function ($users) use ($handle) {
                foreach ($users as $user) {
                    fputcsv($handle, [$user->name, $user->email, $user->created_at]);
                }
            });
            
            fclose($handle);
        }, $filename);
    }
}
@endphp
Share this post:

Related Posts

Comments (0)

Please log in to leave a comment. Log in

No comments yet. Be the first to comment!