An easy to use tool to export an eloquent query results to CSV format.
Add eloquent-exporter to your composer.json
file:
"require": {
"opilo/eloquent-exporter": "~1.0"
}
Use composer to install this package.
$ composer update
Register the service provider within the providers array found in app/config/app.php
:
'providers' => array(
'Opilo\Exporter\ExporterServiceProvider'
)
Add an alias within the aliases array found in app/config/app.php
:
'aliases' => array(
'Exporter' => 'Opilo\Exporter\Facades\Exporter',
)
Next step is to publish package configuration by running:
$ php artisan config:publish opilo/eloquent-exporter
Config file could be found in app/config/packages/opilo/eloquent-exporter/config.php
You can use eloquent-exporter in two way
Make sure to register exporter service manager and add exporter facades to your aliases, then you could do:
<?php
// create your queryBuilder
$query = User::where('status', 1);
Exporter::export($query);
Make sure exporter service manager is registered in your app config.
<?php
// create your queryBuilder
$query = User::where('status', 1);
// create get exporter out of IoC
$exporter = App::make('exporter');
$exporter->export($query);
You can set and filter columns you want to be exported, by passing an array of columns as second parameter to export method.
<?php
// create your queryBuilder
$query = User::where('status', 1);
Exporter::export($query, ['username', 'email', 'name']);
You can also set alias for columns you don't want to be printed as your sql table column name. Just set your alias as value of your table.
<?php
// create your queryBuilder
$query = User::where('status', 1);
Exporter::export($query, ['uname' => 'username', 'email', 'name']);
When you want to export a joint query you should pass a key value array as third parameter of export method. The key should be name of relation, and value is a array that specify the column you want to export from related table.
<?php
// create your queryBuilder
$query = User::where('status', 1)->with('roles');
Exporter::export($query, ['username', 'email', 'name'], ['roles' => ['column' => 'title']]);
The relation fields will join together by what is set in package config as relation_glue
(it is set to |
by default)
You can also set alias for your query relation columns, just specify the alias in relation name value array.
<?php
// create your queryBuilder
$query = User::where('status', 1)->with('roles');
Exporter::export($query, ['username', 'email', 'name'], ['roles' => ['column' => 'title', 'alias' => 'User Roles']]);
field | description | default value |
---|---|---|
chunk_size | Size of chunk of results to be written in file. | 100 |
store_path | Where exported file should be stored. | app/storage/export/ |
file_extension | Extension of exported file | .ext |
relation_glue | Character to join relation fields together | | (pipeline) |
csv_delimiter | Character to delimit columns in csv file | , |