Skip to content
A fast and tiny PHP library to export data to CSV based on Generator. Export millions of data seamlessly without memory exception.
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.

Packagist Version Packagist GitHub


A fast and tiny PHP library to export data to CSV. The library is based on a PHP generator.

Why Use

It took me 5 seconds to export 5M data so you can call it fast enough. And because of the use of Generator it uses less memory and never get caught by memory exception.


composer require sujan/php-csv-exporter

Basic Usage

$columns = [ 'id', 'name', 'email' ];

$queryBuilder = User::limit(10); // Query Builder

$exporter = new Exporter();
$exporter->build($queryBuilder, $columns, 'users.csv')

Build and export, that much simple.


Build CSV

CSV build takes three parameters. First one is the model which could be Array, PDOStatement, Eloquent Query Builder and Collection, seconds one takes the field names you want to export, third one is CSV filename.

$exporter->build($queryBuilder, $columns, 'users.csv');

Export CSV


Usage Examples


You can export data from Eloquent Query Builder, Collection and Array whereas Eloquent Query Builder is highly recommended.

From Eloquent Query Builder (RECOMMENDED)

$columns = [ 'id', 'name', 'email' ];

$queryBuilder = User::latest()->whereNotNull('email_verified_at'); // Query Builder

$exporter = new Exporter();
$exporter->build($queryBuilder, $columns, 'users.csv')

From Collection

$columns = [ 'id', 'name', 'email' ];

$collection = User::latest()->get(); // Collection

$exporter = new Exporter();
$exporter->build($collection, $columns, 'users.csv')

From Array

$columns = [ 'id', 'name', 'email' ];

$usersArray = User::latest()->get()->toArray(); // Array of Users

$exporter = new Exporter();
$exporter->build($usersArray, $columns, 'users.csv')

Eloquent Relation

$columns = [
    'user' => [ // user is a relation

$queryBuilder = Post::with('user'); // Query builder

$exporter = new Exporter();
$exporter->build($queryBuilder, $columns, 'users.csv')


The library supports Laravel as well as raw PHP. You can easily export data from PDOStatement and Array.

From Plain Array

$array = [
    ['id' => 1, 'name' => 'John Doe', 'email' => ''],
    ['id' => 2, 'name' => 'Jane Doe', 'email' => '']

$columns = ['id', 'name', 'email'];

$exporter = new Exporter();
$exporter->build($array, $columns, 'users.csv')


    $servername = "localhost";
    $username = "root";
    $password = "password";
    $dbname = "laravel";

    $columns = [

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("SELECT id, name, email FROM users");

        // set the resulting array to associative

        $exporter = new Exporter();
        $exporter->build($stmt, $columns, 'users.csv)
    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    $conn = null;

You are always welcome to contribute

You can’t perform that action at this time.