Search by

iamankitthapar / laravel-csv-export

A simple Laravel package for exporting arrays, collections, and Eloquent results to CSV files.

Maintainers

Package info

github.com/iamankitthapar/laravel-csv-export

pkg:composer/iamankitthapar/laravel-csv-export

Transparency log

Statistics

Installs: 4

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.2.0 2026-08-08 18:44 UTC

This package is auto-updated.

Last update: 2026-08-08 18:54:40 UTC


README

A simple Laravel package for exporting arrays, collections, generators, and Eloquent query results to CSV files with memory-friendly streaming.

Requirements

  • PHP 8.2+
  • Laravel 11, 12 or 13

Installation

composer require iamankitthapar/laravel-csv-export

Publish configuration

php artisan vendor:publish --tag=csv-export-config

Basic usage

use IamAnkitThapar\LaravelCsvExport\Facades\CsvExport;

return CsvExport::download([
    [
        'id' => 1,
        'name' => 'John Doe',
        'email' => 'john@example.com',
    ],
], 'users.csv');

Export an Eloquent collection

use IamAnkitThapar\LaravelCsvExport\Facades\CsvExport;
use App\Models\User;

return CsvExport::download(
    User::query()
        ->select('id', 'name', 'email')
        ->get(),
    'users.csv'
);

Large dataset exports

Avoid loading an entire table with User::all(). For large exports, pass a Laravel lazy query directly to the exporter. Rows are written as they arrive and are not converted to a normal collection.

lazyById() is recommended for very large, ID-based exports:

return CsvExport::download(
    User::query()
        ->select('id', 'name', 'email')
        ->lazyById(1000),
    'users.csv'
);

The exporter also accepts lazy(), cursor(), LazyCollection, and PHP generators:

return CsvExport::download(
    User::query()
        ->select('id', 'name', 'email')
        ->cursor(),
    'users.csv'
);

For extremely large database results, prefer lazy() or lazyById() over cursor(), as PDO drivers may buffer cursor query results.

Progress callbacks

The optional callback runs after each row is written and receives the one-based row number plus the original source row:

return CsvExport::download(
    data: User::query()->lazyById(1000),
    filename: 'users.csv',
    progress: function (int $row, mixed $user): void {
        if ($row % 1000 === 0) {
            logger()->info("Exported {$row} users");
        }
    }
);

Progress callbacks are available on both download() and store().

Custom headers

return CsvExport::download(
    data: $users,
    filename: 'users.csv',
    headers: [
        'User ID',
        'Full Name',
        'Email Address',
    ]
);

Save to storage

CsvExport::store(
    data: $users,
    path: 'exports/users.csv',
    disk: 'local'
);

Get CSV as a string

$csv = CsvExport::toString($users);

toString() necessarily holds the final CSV text in memory. Use download() or store() for large datasets.

Testing

composer test

License

MIT