fersot/model-export

Export Eloquent queries, collections or arrays to Excel/CSV with a fluent API, column mapping, chunk processing and streaming downloads.

Maintainers

Package info

github.com/fersot/model-export

pkg:composer/fersot/model-export

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-03-26 10:17 UTC

This package is auto-updated.

Last update: 2026-03-26 10:23:04 UTC


README

Export Eloquent queries, Collections, or arrays to Excel or CSV with a fluent API, column mapping, transform callbacks, chunk processing, and streaming HTTP downloads.

Latest Version PHP Version License Tests

Installation

composer require fersot/model-export

Laravel auto-discovers the service provider and facade. Optionally publish the config:

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

Quick Start

use Fersot\ModelExport\Exporter;

// Download directly from a controller
return Exporter::from(User::query())->download('users.xlsx');

// Save to disk
Exporter::from(Order::where('status', 'paid'))->toExcel(storage_path('exports/orders.xlsx'));

// With column mapping
return Exporter::from(User::query())
    ->columns([
        ExportColumn::make('name')->label('Nombre'),
        ExportColumn::make('email')->label('Correo'),
        ExportColumn::make('created_at')->label('Registro')->transform(fn($v) => $v->format('d/m/Y')),
    ])
    ->download('usuarios.xlsx');

Sources

use Fersot\ModelExport\Exporter;

// Eloquent Builder
Exporter::from(User::where('active', 1));

// Illuminate Collection
Exporter::from(collect([['name' => 'Alice'], ['name' => 'Bob']]));

// Plain PHP array
Exporter::from([['product' => 'Widget', 'price' => 9.99]]);

// Model shortcut (uses Model::query())
Exporter::model(User::class);

Fluent Options

Column Mapping

Define columns explicitly with a custom label and optional value transformer:

use Fersot\ModelExport\ExportColumn;

Exporter::from(User::query())
    ->columns([
        ExportColumn::make('name')->label('Nombre'),
        ExportColumn::make('email')->label('Correo Electrónico'),
        ExportColumn::make('age')->label('Edad')->transform(fn($v) => "{$v} años"),

        // The transform also receives the full row as a second argument
        ExportColumn::make('name')->label('Info')
            ->transform(fn($v, $row) => "{$v} <{$row['email']}>"),
    ])
    ->download('users.xlsx');

When ->columns() is used, only the specified columns are exported in the exact order defined.

Filter Columns (without mapping)

// Keep only these columns
Exporter::from(User::query())->only(['name', 'email'])->toArray();

// Exclude columns
Exporter::from(User::query())->except(['password', 'remember_token'])->toArray();

Chunk Processing (large datasets)

Exporter::from(User::query())
    ->chunk(500)           // default: 1000
    ->download('all_users.xlsx');

Output Formats

$exporter = Exporter::from(User::query());

// PHP array / Collection
$exporter->toArray();
$exporter->toCollection();

// Save to file
$exporter->toExcel('path/to/output.xlsx');   // returns the path
$exporter->toCsv('path/to/output.csv');      // returns the path

// HTTP download (StreamedResponse)
$exporter->download('filename.xlsx');
$exporter->downloadCsv('filename.csv');

Macros

After the service provider boots, macros are available on EloquentBuilder and Collection:

// Builder macros
User::where('active', 1)->toExcel(storage_path('exports/active_users.xlsx'));
User::where('active', 1)->toCsv(storage_path('exports/active_users.csv'));

// Return a download response directly
return User::query()->exportDownload('users.xlsx');
return User::query()->exportDownloadCsv('users.csv');

// Collection macros
$collection->toExcel('path/to/file.xlsx');
return $collection->exportDownload('data.xlsx');

HasExport Trait

Add the trait to any Eloquent model for convenient export methods:

use Fersot\ModelExport\Traits\HasExport;

class User extends Model
{
    use HasExport;
}

// Export all records
return User::export()->only(['name', 'email'])->download('users.xlsx');

// Export with a scoped query
return User::exportQuery(fn($q) => $q->where('role', 'admin'))
    ->download('admins.xlsx');

Configuration

After publishing, edit config/model-export.php:

return [
    'chunk_size'     => 1000,
    'disk'           => 'local',
    'default_format' => 'xlsx',
    'temp_path'      => null,   // null = sys_get_temp_dir()
];

Requirements

Dependency Version
PHP ^8.1
Laravel ^9.0 | ^10.0 | ^11.0 | ^12.0
fersot/excel-to ^2.0

Testing

composer install
./vendor/bin/phpunit --testdox

22 tests covering all features.

Author

Hember Colmenareshemberfer@gmail.com · GitHub

Support ☕️

Buy Me a Coffee

License

MIT — see LICENSE.