Skip to content

Instantly share code, notes, and snippets.

@MistrySaurabh
Created September 4, 2018 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MistrySaurabh/f1beb4061fdde12b30aaa7f627b67334 to your computer and use it in GitHub Desktop.
Save MistrySaurabh/f1beb4061fdde12b30aaa7f627b67334 to your computer and use it in GitHub Desktop.
protected function downloadCSV(Request $request){
$table = UserAccount::orderBy('created_at','desc')->with('user')->get();
$filename = "writers.csv";
$handle = fopen($filename, 'w+');
fputcsv($handle, array('user_name','user_email','mobile','account_no','account_holdername','account_type','bank_name','bank_ifsc_code','country','state','address','zip_code','city'));
foreach($table as $row) {
fputcsv($handle, array(
$row['user']['name'],
$row['user_email'],
$row['mobile'],
$row['account_no'],
$row['account_holdername'],
$row['account_type'],
$row['bank_name'],
$row['bank_ifsc_code'],
$row['country'],
$row['state'],
$row['address'],
$row['zip_code'],
$row['city']
));
}
fclose($handle);
$headers = array('Content-Type' => 'text/csv');
return response()->download($filename, 'writers.csv', $headers);
}
protected function downloadJSON(Request $request){
$table = UserAccount::orderBy('created_at','desc')->with('user:id,name')->get();
$filename = "writers.csv";
$handle = fopen($filename, 'w+');
fputs($handle, $table->toJson(JSON_PRETTY_PRINT));
fclose($handle);
$headers = array('Content-type'=> 'application/json');
return response()->download($filename,'writers.json',$headers);
}
protected function downloadEXCEL(Request $request){
$table = UserAccount::orderBy('created_at','desc')->with('user:id,name')->get();
$filename = "writers.xls";
$handle = fopen($filename, 'w+');
fputcsv($handle,array('user_name','user_email','mobile','account_no','account_holdername','account_type','bank_name','bank_ifsc_code','country','state','address','zip_code','city'));
foreach($table as $row) {
fputcsv($handle,array(
$row['user']['name'],
$row['user_email'],
$row['mobile'],
$row['account_no'],
$row['account_holdername'],
$row['account_type'],
$row['bank_name'],
$row['bank_ifsc_code'],
$row['country'],
$row['state'],
$row['address'],
$row['zip_code'],
$row['city']
));
}
fclose($handle);
$headers = array('Content-type'=> 'application/vnd.ms-excel');
return response()->download($filename,'writers.xls',$headers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment