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.
createFromPath, createFromUrl, or createFromString to wrap PHP streams without loading full CSV into memory.$reader
->setFilterCallback(fn(array $row) => count($row) === 5)
->addSortBy(2, SORT_STRING, SORT_DESC)
->setOffset(10)
->setLimit(50)
->fetchAll();
insertOne()/insertAll(), add BOM for Excel compatibility ($writer->setOutputBOM(Writer::BOM_UTF8)), and force downloads via output().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).Storage::disk('local')->readStream() or Symfony’s StreamedResponse seamlessly by passing streams.$writer->setEscape('\\') or '') to avoid deprecation notices.auto_detect_line_endings on (but note: deprecated in PHP 8.1+, removed in 9.0). Use createFromPath with 'r' mode to avoid issues.$reader->getInputBOM(); always output BOM explicitly for Excel compatibility (BOM_UTF8, BOM_UTF16_LE).detectDelimiterList() (not detectDelimiter())—it never throws; returns array of candidates, so handle ambiguity gracefully.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.fetchOne(), fetchAssoc(), fetchColumn(), or generators over fetchAll() for large files.setEncoding → setEncodingFrom (v6), getReader()/getWriter() → newReader()/newWriter(). Always check CHANGELOG when upgrading major versions.How can I help you explore Laravel packages today?