Reputation: 93
I'm writing a artisan command to export the table from my database as a csv file. But when im running the command, the CLI shows me only the data from the database and don't create a CSV File.
Upvotes: 1
Views: 5807
Reputation: 678
You could try this library: https://github.com/box/spout.
Allows you to create CSV, XLSX, ODS files easily like this:
use Box\Spout\Writer\WriterFactory;
use Box\Spout\Common\Type;
$writer = WriterFactory::create(Type::XLSX); // for XLSX files
//$writer = WriterFactory::create(Type::CSV); // for CSV files
//$writer = WriterFactory::create(Type::ODS); // for ODS files
$writer->openToFile($filePath); // write data to a file or to a PHP stream
//$writer->openToBrowser($fileName); // stream data directly to the browser
$writer->addRow($singleRow); // add a row at a time
$writer->addRows($multipleRows); // add multiple rows at a time
$writer->close();
You will just pass the rows as arrays and that's it.
You can find the documentation here: http://opensource.box.com/spout/
Upvotes: 1
Reputation: 462
this works fine in my laravel 5.4 project
public function handle()
{
$filename = "logins.csv";
$handle = fopen($filename, 'w');
fputcsv($handle, array('customer_id', 'count', 'year', 'month', 'day', 'hour'));
fputcsv($handle, array("row['customer_id']", "row['count']", "row['year']", "row['month']", "row['day']", "row['hour']"));
fclose($handle);
$headers = array(
'Content-Type' => 'text/csv',
);
}
creates file logins.csv in the project root directory
customer_id,count,year,month,day,hour
row['customer_id'],row['count'],row['year'],row['month'],row['day'],row['hour']
btw what is purpose of $headers array ?
CLI shows me only the data from the database
maybe you have dd() somewhere?
Upvotes: 2