3

Can't find in docs of Laravel Excel how to redirect() after download(). I've tried to do smth like this

Excel::load('/bills/bill.template.xlsx', function($doc) {
    $sheet = $doc->setActiveSheetIndex(0);
    $sheet->setCellValue('G21', '{buyer}');
})->download('xlsx');

return redirect('/')->with('notification','custom msg');

but it doesn't work.

Arsen Ibragimov
  • 425
  • 4
  • 18

2 Answers2

1

It seems it can't be done but there's a workaround you could try on a similar question:

How do I redirect after download in Laravel?

Which basically you redirect back to the final page first then initiate the download:

Session::flash('download.in.the.next.request', 'filetodownload.pdf');

return Redirect::to('/');

Then on your view would look something like this:

<html>
  <head>
      @if(Session::has('download.in.the.next.request'))
         <meta http-equiv="refresh" content="5;url={{     Session::get('download.in.the.next.request') }}">
      @endif
   <head>

   <body>
      ...
   </body>
</html>

Checkout the above link for further explanation.

Community
  • 1
  • 1
dmotors
  • 611
  • 2
  • 7
  • 19
0

To download you have to execute the return statement along with the download.

return Excel::load('/bills/bill.template.xlsx', function($doc) {
    $sheet = $doc->setActiveSheetIndex(0);
    $sheet->setCellValue('G21', '{buyer}');
})->download('xlsx');

It wont download itself if you don't send it to the view. So, there is no such a thing as download and redirect as far as I know.

What you can do is to store the Excel file somewhere in the storage folder of laravel (or wherever you like), and then redirect. In the new page you can open a hidden page with JS and in that one, download the excel file you want to give to the user.