Can't Download File as CSV
I'm trying to export some data as CSV & every time i try to export data i got this error - Call to undefined method Symfony\Component\HttpFoundation\StreamedResponse::header()
I have tried different possible ways but they aren't working
My Function (Controller File)
public function exportd(Request $request)
{
$fileName = 'data.csv';
$data = json_decode($request->data);
$headers = array(
"Content-type" => "text/csv",
"Content-Disposition" => "attachment; filename=$fileName",
"Pragma" => "no-cache",
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
"Expires" => "0"
);
$columns = array('Name', 'Gender', 'experrience', 'location', 'Pref Location', 'Salary'
...
);
$callback = function() use($data, $columns) {
$file = fopen('php://output', 'w');
fputcsv($file, $columns);
foreach ($data as $da) {
$row['Name'] = $da->name;
$row['Gender'] = $da->gender;
$row['experrience'] = $task->experrience;
$row['Pref Location'] = $task->pref_loc;
$row['Salary'] = $task->salary;
...
fputcsv($file, array($row['Name'], $row['Gender'],$row['experrience'], $row['location'], $row['Pref Location'], $row['Salary']
...
));
}
fclose($file);
};
return Response()->stream($callback, 200, $headers);
}
My Middleware (AuthCheck)
public function handle(Request $request, Closure $next)
{
if(!session()->has('LoggedUser') && ($request->path() !='auth/login' && $request->path() !='auth/register' )){
return redirect('auth/login')->with('fail','You must be logged in');
}
if(session()->has('LoggedUser') && ($request->path() == 'auth/login' || $request->path() == 'auth/register' ) ){
return back();
}
return $next($request)->header('Cache-Control','no-cache, no-store, max-age=0, must-revalidate')
->header('Pragma','no-cache')
->header('Expires','0');
}