How do I redirect to another page for successfully exporting a laravel view file. I am using Laravel Excel 2.1.20. I researched and I discovered that this can't be done except I redirect first and then download the excel sheet. I did the below but still does not work.
Here is my controller:
$export = Excel::create('Request for Quote', function($excel) use($item, $request) {
$excel->sheet('RFQ 1', function($sheet) use($item, $request) {
$sheet->loadView('requests.send_rfq_pdf')
->with('item', $item)
->with('request', $request);
});
});
session(['download.in.the.next.request' => $export]);
return back();
My View:
@if(Session::has('download.in.the.next.request'))
<meta http-equiv="refresh" content="5;url={{Session::get('download.in.the.next.request') }}">
@endif
If you trigger the download of the Excel file via a POST
request, using a closure like ->download("xlsx");
, your browser will not have to redirect anywhere.
For example:
// ExampleController.php
public function postDownload(Request $request){
Excel::create('Request for Quote', function($excel) use($item, $request) {
$excel->sheet('RFQ 1', function($sheet) use($item, $request) {
$sheet->loadView('requests.send_rfq_pdf')
->with('item', $item)
->with('request', $request);
});
})->download("xlsx");
}
You would need a route
to handle this post request:
// routes.php:
Route::post("/pdf/download", "ExampleController@postDownload");
and a view
with a simple form to generate a POST
request to the route defined if your routes.php
file:
<!-- {view}.blade.php -->
<form method="POST" action="{{ url('/pdf/download') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}/>
<button type="submit">Download</button>
</form>