PHPackages                             elrayes/laravel-csv-export - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. elrayes/laravel-csv-export

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

elrayes/laravel-csv-export
==========================

Lightweight CSV export package for Laravel with exporters, service, writer, and facade.

v1.0.1(4mo ago)08MITPHPPHP &gt;=8.2

Since Jan 6Pushed 4mo agoCompare

[ Source](https://github.com/Ahmed-Elrayes/laravel-csv-export)[ Packagist](https://packagist.org/packages/elrayes/laravel-csv-export)[ Docs](https://github.com/Ahmed-Elrayes/laravel-csv-export)[ RSS](/packages/elrayes-laravel-csv-export/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (3)Used By (0)

Elrayes/LaravelCsvExport
========================

[](#elrayeslaravelcsvexport)

Modern, clean CSV export for Laravel 12+ with separation of concerns:

- Contracts for defining what to export
- Exporters to encapsulate query + mapping
- Services to orchestrate writing
- A Facade for ergonomic usage

Licensed under MIT.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require elrayes/laravel-csv-export
```

The service provider and facade will be automatically registered via Laravel's package discovery.

Concepts
--------

[](#concepts)

- Elrayes\\LaravelCsvExport\\Contracts\\DataExporterInterface
    - query(): Builder|Collection
    - headings(): array
    - map(mixed $row): array
- Elrayes\\LaravelCsvExport\\Exporters\\BaseExporter
    - Sensible defaults + configuration helpers: chunk size, max limit, include BOM
- Elrayes\\LaravelCsvExport\\Services\\CSVExportService
    - exportToHandle(): core routine using CSVWriter
    - toFile(): write to a path
    - stream(): streamed download
    - download(): generate and return a downloadable response
    - store(): NEW — store resulting CSV to a Laravel Storage disk
- Elrayes\\LaravelCsvExport\\Services\\CSVWriter
    - Low-level wrapper around fputcsv (writeBom, writeRow, close)
- Elrayes\\LaravelCsvExport\\Facades\\CSVExport
    - Facade accessor for easy calls

Quick start
-----------

[](#quick-start)

Generate an exporter using the Artisan command:

```
php artisan make:export UserCSVExporter
```

This will create `app/Export/UserCSVExporter.php`. Implement the logic in the generated class.

Example exporter:

```
namespace App\Export;

use App\Models\User;
use Elrayes\LaravelCsvExport\Exporters\BaseExporter;
use Illuminate\Contracts\Database\Eloquent\Builder as BuilderContract;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;

class UserCSVExporter extends BaseExporter
{
    protected bool $includeBom = true; // Excel-friendly

    public function query(): Builder|Collection|BuilderContract
    {
        return User::query()->select(['id', 'name', 'email', 'created_at']);
    }

    public function headings(): array
    {
        return ['ID', 'Name', 'Email', 'Registered At'];
    }

    public function map(mixed $row): array
    {
        return [
            $row->id,
            $row->name,
            $row->email,
            optional($row->created_at)->toDateTimeString(),
        ];
    }
}
```

Usage
-----

[](#usage)

Import the Facade:

```
use Elrayes\LaravelCsvExport\Facades\CSVExport;
use App\Exporters\UserCSVExporter;
```

- Write to a specific file path:

```
$path = storage_path('app/exports/users.csv');
CSVExport::toFile(UserCSVExporter::class, $path);
```

- Stream as a download (controller action):

```
return CSVExport::stream(UserCSVExporter::class, 'users.csv');
```

- Using Facade configuration overrides (Fluent API):

```
return CSVExport::setChunkSize(2000)
    ->setMaxLimit(50000)
    ->includeBom(true)
    ->stream(UserCSVExporter::class, 'users.csv');
```

- Download after generating a temp file:

```
return app(Elrayes\LaravelCsvExport\Services\CSVExportService::class)
    ->download(UserCSVExporter::class, 'users.csv');
```

- Store to a Laravel Storage disk (new):

```
// Store on S3 at exports/users.csv
$stored = CSVExport::store(UserCSVExporter::class, 'exports/users.csv', 's3');
// returns 'exports/users.csv' if successful
```

Configuration knobs
-------------------

[](#configuration-knobs)

From BaseExporter (or set dynamically on your exporter instance):

- chunk size: how many rows to process per chunk
- max limit: cap total rows to export (null for unlimited)
- use max limit: toggle applying the cap
- include BOM: prepend UTF-8 BOM for Excel compatibility

Example:

```
$exporter = app(UserCSVExporter::class)
    ->setChunkSize(2000)
    ->setMaxLimit(50000)
    ->setUseMaxLimit(true)
    ->setIncludeBom(true);

// When resolving via the Facade, you can bind a configured instance in the container
app()->bind(UserCSVExporter::class, fn () => $exporter);

CSVExport::stream(UserCSVExporter::class, 'users.csv');
```

Error handling
--------------

[](#error-handling)

Exceptions thrown during writing propagate after being reported via Laravel's exception handler (when using the app). Always wrap in try/catch at call sites if you want custom failure behavior.

License
-------

[](#license)

MIT © Elrayes

Advanced typing (PHPDoc generics)
---------------------------------

[](#advanced-typing-phpdoc-generics)

For better static analysis (e.g., with Larastan/PHPStan), the package exposes a generic type parameter for the row model passed to map().

- DataExporterInterface is declared as `@template TModel`
- BaseExporter implements `@implements DataExporterInterface`
- map() is documented as `@param TModel $row`
- query() is documented as returning `Builder|Collection|BuilderContract`

Example with a User model:

```
use Elrayes\LaravelCsvExport\Exporters\BaseExporter;
use App\Models\User;
use Illuminate\Contracts\Database\Eloquent\Builder as BuilderContract;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;

/**
 * @extends BaseExporter
 */
class UserCSVExporter extends BaseExporter
{
    public function query(): Builder|Collection|BuilderContract
    {
        return User::query()->select(['id', 'name', 'email', 'created_at']);
    }

    public function headings(): array
    {
        return ['ID', 'Name', 'Email', 'Registered At'];
    }

    /** @param User $row */
    public function map(mixed $row): array
    {
        return [
            $row->id,
            $row->name,
            $row->email,
            optional($row->created_at)->toDateTimeString(),
        ];
    }
}
```

###  Health Score

37

—

LowBetter than 82% of packages

Maintenance82

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

123d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/afbd98b02f1a8aec1f3d13cdb96c27a3baebb6872848bcf23cab1812d8ca072b?d=identicon)[Elrayes](/maintainers/Elrayes)

---

Top Contributors

[![Ahmed-Elrayes](https://avatars.githubusercontent.com/u/30704271?v=4)](https://github.com/Ahmed-Elrayes "Ahmed-Elrayes (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/elrayes-laravel-csv-export/health.svg)

```
[![Health](https://phpackages.com/badges/elrayes-laravel-csv-export/health.svg)](https://phpackages.com/packages/elrayes-laravel-csv-export)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M683](/packages/barryvdh-laravel-ide-helper)[spatie/laravel-enum

Laravel Enum support

3655.4M31](/packages/spatie-laravel-enum)[psalm/plugin-laravel

Psalm plugin for Laravel

3274.9M308](/packages/psalm-plugin-laravel)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M157](/packages/orchestra-canvas)[illuminate/pipeline

The Illuminate Pipeline package.

9446.6M210](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10532.5M857](/packages/illuminate-pagination)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
