I am generating CSV file with extension of .csv
Here is my simple code:
Here is my simple code:
public function download(){
$headers = [
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0'
, 'Content-type' => 'application/vnd.ms-excel; charset=utf-8'
, 'Content-Disposition' => 'attachment; filename=users.csv'
, 'Expires' => '0'
, 'Pragma' => 'public'
];
$users = Users::where('status', '=', '1')->get();
$columns = array('No','User ID','Name','Email','Mobile','Register Date');
$callback = function() use ($users, $columns)
{
$file = fopen('php://output', 'w');
fputcsv($file, $columns);
foreach($users as $index => $user) {
fputcsv($file, array($index+1,$user->id, $user->name,$user->email,$user->mobile,date('d/m/Y', strtotime($user->created_at))));
}
fclose($file);
};
return Response::stream($callback, 200, $headers);
}
No comments:
Post a Comment