A fast and tiny PHP library to export data to CSV. The library is based on a PHP generator.
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
$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.
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');
$exporter->export();
You can export data from Eloquent Query Builder
, Collection
and Array
whereas Eloquent Query Builder
is highly recommended.
$columns = [ 'id', 'name', 'email' ];
$queryBuilder = User::latest()->whereNotNull('email_verified_at'); // Query Builder
$exporter = new Exporter();
$exporter->build($queryBuilder, $columns, 'users.csv')
->export();
$columns = [ 'id', 'name', 'email' ];
$collection = User::latest()->get(); // Collection
$exporter = new Exporter();
$exporter->build($collection, $columns, 'users.csv')
->export();
$columns = [ 'id', 'name', 'email' ];
$usersArray = User::latest()->get()->toArray(); // Array of Users
$exporter = new Exporter();
$exporter->build($usersArray, $columns, 'users.csv')
->export();
$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();
The library supports Laravel as well as raw PHP. You can easily export data from PDOStatement
and 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();
$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;