Skip to content

[Feature] CSV/Excel Export #4

Description

@devalade

Description

Add export functionality to the generated index page.

Proposed Syntax

php artisan crudify:generate Product --fields="name:string,price:decimal" --export

Features

  • Export filtered/sorted results to CSV
  • Export to Excel (via maatwebsite/excel or spatie/simple-excel)
  • Select columns to export
  • Export with relationships (e.g., include user name instead of user_id)
  • Background export for large datasets (queue job)

Generated Code

// In Livewire Index
public function export(string $format = 'csv'): StreamedResponse
{
    return response()->streamDownload(function () use ($format) {
        $query = Product::query()->with(['user'])->when($this->search, ...);
        
        if ($format === 'csv') {
            $file = fopen('php://output', 'w');
            fputcsv($file, ['Name', 'Price', 'User']);
            foreach ($query->cursor() as $product) {
                fputcsv($file, [$product->name, $product->price, $product->user?->name]);
            }
            fclose($file);
        }
    }, "products-".now()->format('Y-m-d').".csv");
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions