Lucas Campos Lucas Campos - 5 months ago 27
PHP Question

How do I redirect after download in Laravel?

I'm using maatwebsite/excel library to create excel files then download my file

in my controller I do like this:

public function todas()
$input = Input::all();

$datainicio = DB::table('tb_periodo')->where('cod', $input['todos'])->pluck('periodo_inicio');
$datafinal = DB::table('tb_periodo')->where('cod', $input['todos'])->pluck('periodo_fim');
$mes = DB::table('tb_periodo')->where('cod', $input['todos'])->pluck('mes_referencia');

$horarioQuery = $this->horario->with(array('funcionario', 'item_contabil'))
->whereBetween('data', array($datainicio, $datafinal))
->orderBy('data', 'ASC')

$horarios = reset($horarioQuery);

$count = count($horarios);

return Redirect::route('relatorios.todas')->withErrors(['não existem marcações para este período']);

$funcionarios = $this->funcionario->all();

$datainicio = Carbon::createFromFormat('Y-m-d H:i:s', $datainicio);
$datafinal = Carbon::createFromFormat('Y-m-d H:i:s', $datafinal);

$nome = 'Marcações'.$mes.'-'.Carbon::now()->year;
$this->horario->allToExcel($nome, $horarios);

return View::make('relatorios.todashow', compact('funcionarios', 'datainicio', 'datafinal', 'horarios', 'count', 'nome'));
return Redirect::route('relatorios.todas')->withErrors(['Selecione um período!']);

that is my function to generate excel file :

public static function allToExcel($nome, $horarios)
Excel::create($nome , function ($excel) use ($horarios) {

$excel->sheet('Marcações', function ($sheet) use ($horarios) {

$sheet->row(1, array(

'Hora Faturada',
'Hora Contratada',
'Horas Trabalhadas',
'Horas Extras',
'Tempo Exposicao',
'Item Contabil',

$sheet->row(1, function($row) {

$i = 2;
foreach ($horarios as $horario) {

if($horario->funcionario->funcao_qt != null)
$funcao = $horario->funcionario->funcao_qt;
$funcao = $horario->funcionario->funcao_a5;

$sheet->row($i, array(
$horario->tempo_exposicao ?: "-",
if($i % 2 != 0)
$sheet->row($i, function($row) {



but after I download the excel file I cant redirect to a route or view and I Also tried to use :


Route::post('relatorios/todas', array('as' => 'relatorios.todas', 'after' => 'voltar', 'uses' => 'ReportsController@todas'));


Route::filter('voltar', function()
return Redirect::back()->with('message', '<p class="bg-success"><b>Relatório gerado com sucesso</b></p>');

but didnt work anyway, have another way to redirect after download my file ??!

thx in advance!


It cannot be done. The problem is that if you send a download instruction to the user browser, you are, as a matter of fact, sending a response and you can send only one response back.

What you could do is to, first redirect your user to the "final" page and in that page start the download. The code would be something like:

Session::flash('', 'filetodownload.pdf');

return Redirect::to('/whatever/page');

Then in your new page you'll have some options:

So you can in your layout do something like:

         <meta http-equiv="refresh" content="5;url={{ Session::get('') }}">


Also, take a look at this answer: PHP generate file for download then redirect