Weave 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

Csv Bundle Laravel Package

chaplean/csv-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require chaplean/csv-bundle
    

    Ensure your composer.json includes the package under require.

  2. Bundle Registration: Add to config/bundles.php (Laravel 5.5+):

    return [
        // ...
        Chaplean\Bundle\CsvBundle\ChapleanCsvBundle::class => ['all' => true],
    ];
    

    (Note: Adjust namespace if the bundle uses a different one.)

  3. First Use Case: Generate a CSV from a Laravel Eloquent collection:

    use Chaplean\Bundle\CsvBundle\CsvExporter;
    
    $users = User::all();
    $exporter = new CsvExporter();
    $csvContent = $exporter->export($users, ['name', 'email']);
    file_put_contents('users.csv', $csvContent);
    

Key Files to Review

  • Service Configuration: Check for config/packages/chaplean_csv.yaml (if provided).
  • Exporter Class: Inspect CsvExporter methods (e.g., export(), setHeaders()).
  • Dependency Injection: Verify if the bundle registers services in Resources/config/services.xml.

Implementation Patterns

Core Workflows

  1. Exporting Collections:

    $exporter = $this->container->get('chaplean_csv.exporter');
    $csv = $exporter->export($collection, ['column1', 'column2']);
    
    • Use setHeaders() to customize column names:
      $exporter->setHeaders(['Full Name' => 'name', 'User Email' => 'email']);
      
  2. Streaming Large Datasets:

    $exporter->stream($collection, ['id', 'created_at'])
        ->setCallback(function ($item) {
            return [$item->id, $item->created_at->format('Y-m-d')];
        })
        ->saveTo('large_file.csv');
    
    • Ideal for >10K rows to avoid memory issues.
  3. Custom Mappers:

    $exporter->setMapper(new CustomMapper());
    class CustomMapper implements MapperInterface {
        public function map($item) {
            return [$item->formattedAttribute(), $item->value];
        }
    }
    

Integration Tips

  • Laravel Service Provider: Bind the exporter in AppServiceProvider:

    $this->app->bind('chaplean_csv.exporter', function () {
        return new CsvExporter();
    });
    
  • API Responses:

    return response()->streamDownload(
        function () use ($exporter, $data) {
            echo $exporter->export($data, ['key']);
        },
        'export.csv'
    );
    
  • Queue Jobs:

    class ExportCsvJob implements ShouldQueue {
        public function handle() {
            $exporter = app('chaplean_csv.exporter');
            $exporter->export($this->data, $this->columns)
                     ->saveTo(storage_path('app/exports.csv'));
        }
    }
    

Gotchas and Tips

Common Pitfalls

  1. Namespace Conflicts:

    • The bundle may use Chaplean\Bundle\CsvBundle; verify autoloading with:
      composer dump-autoload
      
  2. Memory Limits:

    • For large exports (>50K rows), use streaming or chunking:
      $exporter->setChunkSize(1000);
      
  3. Encoding Issues:

    • Force UTF-8 encoding in the exporter:
      $exporter->setEncoding('UTF-8');
      
  4. Missing Dependencies:

    • Ensure league/csv is installed (if the bundle relies on it):
      composer require league/csv
      

Debugging Tips

  • Log Exporter Output:
    $exporter->setLogger($this->app->make('logger'));
    
  • Validate Headers:
    if (!$exporter->validateHeaders($headers)) {
        throw new \InvalidArgumentException('Invalid headers');
    }
    

Extension Points

  1. Custom Writers: Override CsvWriter to add features (e.g., Excel compatibility):

    class ExcelWriter extends CsvWriter {
        public function setExcelFormat() { /* ... */ }
    }
    
  2. Event Listeners: Hook into export events (if the bundle supports them):

    $exporter->on('export.start', function () {
        Log::info('Export initiated');
    });
    
  3. Configuration Overrides: Extend the bundle’s config in config/packages/chaplean_csv.yaml:

    chaplean_csv:
        default_encoding: 'UTF-8'
        chunk_size: 2000
    
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.
gmostafa/php-graphql-client
xeviant/paystack
snowsoft/faker
meditationsteps/blogetc
animalyoung/laravel-influxdb
onix-solutions/sms-telera
lonely-lights/prosetta
oxygen/data
oxygen/core
jengo/base
xiloryx/log-collector
a21ns1g4ts/filament-stripe
la-php/laravel-admin
artisan-build/community-prompts
vleroy/smart
ayles-software/laravel-australian-public-holidays
cboxdk/laravel-id-spatie
cboxdk/laravel-id-billing
cboxdk/laravel-id-billing-bridge
simple-as-fuck/php-performance-log