Im trying to figure out a way i can return a view but also call the download function. Something like this:
return view('home')->download($filename, 'text.csv', $headers);
I don't think that it's possible. Laravel has to choose what to send to your browser. If it sends a redirect header you won't see the page.
The only solution is to render the view and in the view put a simple javascript like:
window.location.href = "<your file url>"
If this is a file it should start a download without changing page.
Currently there's a way to do it using a stream download. It's not pretty, but it works. Note the function has to echo
and not return
, hence why render()
is called on the view.
return response()->streamDownload(function() {
echo view('home')->render();
}
, 'test.csv'
, ['Content-Type' => 'text/csv']
);