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

Csv Laravel Package

league/csv

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer:

composer require league/csv:^9.0

Import Reader or Writer classes and use named constructors — avoid new — to instantiate:

use League\Csv\Reader;
use League\Csv\Writer;

$reader = Reader::createFromPath('/path/to/file.csv', 'r');
$writer = Writer::createFromPath('php://output', 'w');

Key first use case: read a CSV with headers → map rows to objects or arrays:

$records = $reader->setOffset(1)->fetchAssoc(0); // key by first column
// or
$records = $reader->setOffset(1)->fetchAll();

Check the official docs at csv.thephpleague.com for deep examples and API reference.

Implementation Patterns

  • Stream-first workflow: Use createFromPath, createFromUrl, or createFromString to wrap PHP streams without loading full CSV into memory.
  • Pipeline processing: Chain filters and modifiers:
    $reader
        ->setFilterCallback(fn(array $row) => count($row) === 5)
        ->addSortBy(2, SORT_STRING, SORT_DESC)
        ->setOffset(10)
        ->setLimit(50)
        ->fetchAll();
    
  • Export workflows: Map arrays to CSV with insertOne()/insertAll(), add BOM for Excel compatibility ($writer->setOutputBOM(Writer::BOM_UTF8)), and force downloads via output().
  • Transcoding pipelines: Use stream_filter_register() and appendStreamFilter() (7.0+), or the provided FilterTranscode in examples for charset conversion (e.g., UTF-8 → UTF-16 LE for Excel on macOS).
  • Framework-agnostic integration: Use with Laravel’s Storage::disk('local')->readStream() or Symfony’s StreamedResponse seamlessly by passing streams.

Gotchas and Tips

  • PHP 8.4+: Explicitly set the escape character ($writer->setEscape('\\') or '') to avoid deprecation notices.
  • Line endings: Legacy Mac CSVs (carriage return only) require auto_detect_line_endings on (but note: deprecated in PHP 8.1+, removed in 9.0). Use createFromPath with 'r' mode to avoid issues.
  • BOM handling: Detect input BOM with $reader->getInputBOM(); always output BOM explicitly for Excel compatibility (BOM_UTF8, BOM_UTF16_LE).
  • Delimiter detection: Use detectDelimiterList() (not detectDelimiter())—it never throws; returns array of candidates, so handle ambiguity gracefully.
  • Streaming caution: Writer opened in append mode ('a') should be rewound manually if you need to re-read. Prefer 'w' or 'w+' for writing and use newReader()/newWriter() to cross-operate.
  • Memory safety: Prefer fetchOne(), fetchAssoc(), fetchColumn(), or generators over fetchAll() for large files.
  • Upgrading: Renamed methods like setEncodingsetEncodingFrom (v6), getReader()/getWriter()newReader()/newWriter(). Always check CHANGELOG when upgrading major versions.
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.
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
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai