I have come out to the difficult part
public function handle(Request $request, Closure $next)
{
if ($request->ajax()) {
$length = $request->query('length');
if ($length == "-1") {
$response = $next($request);
// dd($response->original);
$headers = array(
"Content-type" => "text/csv",
"Content-Disposition" => "attachment; filename=\"data.csv\"",
"Pragma" => "no-cache",
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
"Expires" => "0"
);
$callback = function () use ($response) {
$out = fopen("php://output", 'w');
fputcsv($out, array_keys($response->original['data'][0]), "\t");
foreach ($response->original['data'] as $data) {
// dd(array_values($data));
fputcsv($out, array_values($data), "\t");
}
fclose($out);
};
return response()->stream($callback, 200, $headers);
}
}
return $next($request);
}
I have played with the data until I get the file BUT the file can not be downloaded through ajax request?
could I redirect to get the file ??