Skip to content

Instantly share code, notes, and snippets.

@ryzr
Created December 1, 2015 00:21
Show Gist options
  • Save ryzr/82bfcdccd2e8bfc8dd44 to your computer and use it in GitHub Desktop.
Save ryzr/82bfcdccd2e8bfc8dd44 to your computer and use it in GitHub Desktop.
$file_name = 'anything';
$rel_path = 'uploads/' . str_slug($file_name) . '.csv';
$full_path = public_path($rel_path);
$f = fopen($full_path, 'w');
$firstLineKeys = false;
Model::select('data', 'data2')->chunk(500, function($entries) use ($f, $firstLineKeys) {
$array = json_decode(json_encode($entries), true);
foreach ($array as $line) {
if (empty($firstLineKeys)) {
$firstLineKeys = array_keys($line);
fputcsv($f, $firstLineKeys);
$firstLineKeys = array_flip($firstLineKeys);
}
fputcsv($f, $line);
}
});
return Redirect::to('/' . $rel_path);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment