Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Dataexporter Bundle Laravel Package

ee/dataexporter-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require ee/dataexporter-bundle
    

    Add to config/bundles.php:

    return [
        // ...
        EE\DataExporterBundle\EEDataExporterBundle::class => ['all' => true],
    ];
    
  2. First Export (CSV)

    use EE\DataExporterBundle\Exporter\CsvExporter;
    use EE\DataExporterBundle\Writer\StreamWriter;
    
    $exporter = new CsvExporter();
    $writer = new StreamWriter('php://output', 'w');
    $exporter->export($writer, ['headers' => ['ID', 'Name'], 'data' => [[1, 'John'], [2, 'Jane']]]);
    
  3. Key Files to Review

    • Resources/doc/index.md (official docs)
    • src/Exporter/ (core exporters: CsvExporter, XmlExporter, etc.)
    • src/Writer/ (writers for files, streams, or in-memory)

Implementation Patterns

Core Workflow

  1. Data Preparation Normalize data into an associative array with headers and data keys:

    $data = [
        'headers' => ['user_id', 'email', 'created_at'],
        'data' => [
            [1, 'john@example.com', '2023-01-01'],
            [2, 'jane@example.com', '2023-01-02'],
        ],
    ];
    
  2. Exporter Selection Choose an exporter based on output format:

    $exporter = match ($format) {
        'csv' => new CsvExporter(),
        'xml' => new XmlExporter(),
        'json' => new JsonExporter(),
        'html' => new HtmlExporter(),
        'xls' => new XlsExporter(), // Requires PhpSpreadsheet
        default => throw new \InvalidArgumentException('Unsupported format'),
    };
    
  3. Writer Integration

    • File Output:
      $writer = new StreamWriter('path/to/file.csv', 'w');
      
    • In-Memory (for APIs):
      $writer = new MemoryWriter();
      $exporter->export($writer, $data);
      $output = $writer->getContent(); // Return as response
      
    • Stream (for downloads):
      $writer = new StreamWriter('php://output', 'w');
      header('Content-Type: text/csv');
      header('Content-Disposition: attachment; filename="export.csv"');
      
  4. Laravel-Specific Integration Use a Service Provider to bind exporters:

    $this->app->bind('exporter.csv', function () {
        return new CsvExporter();
    });
    

    Then inject via constructor:

    public function __construct(private CsvExporter $csvExporter) {}
    
  5. Batch Processing For large datasets, use chunking with QueryBuilder:

    $exporter = new CsvExporter();
    $writer = new StreamWriter('php://output', 'w');
    $exporter->setBatchSize(1000); // Process 1000 rows at a time
    $exporter->export($writer, $data);
    

Gotchas and Tips

Common Pitfalls

  1. Memory Limits

    • Issue: Large exports (e.g., 100K+ rows) may hit PHP’s memory_limit.
    • Fix: Use MemoryWriter with chunking or stream directly to a file:
      $writer = new StreamWriter('php://temp', 'w+');
      
  2. XLS Exporter Dependencies

    • Issue: XlsExporter requires phpoffice/phpspreadsheet (not auto-installed).
    • Fix: Install manually:
      composer require phpoffice/phpspreadsheet
      
  3. Header Injection

    • Issue: Malicious headers/data can break exports (e.g., CSV injection).
    • Fix: Sanitize headers/data before passing to exporter:
      $headers = array_map('htmlspecialchars', $headers);
      
  4. Encoding Issues

    • Issue: Special characters (e.g., é, ü) may corrupt output.
    • Fix: Set charset in writer:
      $writer = new StreamWriter('file.csv', 'w');
      $writer->setCharset('UTF-8');
      
  5. XML/HTML Escaping

    • Issue: Unescaped data in XML/HTML breaks parsing.
    • Fix: Use XmlExporter::setEscapeHtml(true) or pre-escape data:
      $data['data'] = array_map(function ($row) {
          return array_map('htmlspecialchars', $row);
      }, $data['data']);
      

Pro Tips

  1. Dynamic Headers Use get_headers() from Eloquent collections:

    $headers = array_keys($users->first()->toArray());
    $data = ['headers' => $headers, 'data' => $users->toArray()];
    
  2. Custom Writers Extend AbstractWriter for custom storage (e.g., S3, database):

    class S3Writer extends AbstractWriter {
        public function write($data) {
            Storage::disk('s3')->put('export.csv', $data);
        }
    }
    
  3. Laravel Response Helper Create a helper to stream exports:

    function exportToResponse($data, $format) {
        $exporter = match ($format) {
            'csv' => new CsvExporter(),
            // ...
        };
        $writer = new StreamWriter('php://output', 'w');
        $exporter->export($writer, $data);
        return response()->stream();
    }
    
  4. Debugging

    • Log Exports: Redirect StreamWriter to a temp file for debugging:
      $writer = new StreamWriter(storage_path('app/debug_export.csv'), 'w');
      
    • Validate Data: Use DataExporterBundle\Validator\DataValidator to check structure:
      $validator = new DataValidator();
      if (!$validator->validate($data)) {
          throw new \RuntimeException('Invalid export data');
      }
      
  5. Performance

    • Disable indexing for large CSV exports:
      $exporter = new CsvExporter();
      $exporter->setIndexedBy(false); // Faster for unordered data
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle