/php-csv-exporter

A fast and tiny PHP library to export data to CSV based on Generator. Export millions of data seamlessly without memory exception.

Primary LanguagePHPMIT LicenseMIT

Packagist Version Packagist GitHub

Overview

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.

Installation

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')
         ->export();

Build and export, that much simple.

Documentation

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

$exporter->export();

Usage Examples

Laravel

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')
         ->export();

From Collection

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

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

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

From Array

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

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

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

Eloquent Relation

$columns = [
    'id',
    'title',
    'user' => [ // user is a relation
        'name'
    ]
];

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

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

Raw PHP

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' => 'john@example.com'],
    ['id' => 2, 'name' => 'Jane Doe', 'email' => 'jane@example.com']
];

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

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

From PDOStatement (RECOMMENDED)

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

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

    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");
        $stmt->execute();

        // set the resulting array to associative
        $stmt->setFetchMode(PDO::FETCH_ASSOC);

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

You are always welcome to contribute