Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pouya's avatar
Level 1

Laravel Does Not Download Excel File Automatically After Redirecting

I'm suing "maatwebsite/excel": "^3.1" with Laravel 8 and I have made an Export class for downloading an Excel file which is working fine.

And the route which calls this request goes like this:

Route::get('/export-excel',[BatchController::class,'exportIntoExcel'])->name('batch.export.excel');

So at the Controller method, I tried redirecting to route batch.export.excel after an insertion process:

public function priceChanges($pcode, $sprice, $fprice, $seller)
    {
        $insertChanges = \App\Models\Robo\PriceChange::create([
            'product_code' => $pcode,
            'before_update' => $sprice,
            'rival_price' => $fprice,
            'rival_name' => $seller,
            'change_price' => "100",
            'after_update' => 0,
        ]);

        return redirect(route('batch.export.excel'));  
    }

So it should be downloading the Excel file when calling redirect(route('batch.export.excel') but it does not somehow.

However if I manually go to export-excel url on browser, the file gets downloaded properly.

So how to download this Excel file automatically after a Controller method ran ?

0 likes
3 replies
Sinnbeck's avatar

So what happens when it redirects? Does the url change to the correct "Download" page? or?

pouya's avatar
Level 1

No it stays at the same url that I run the Controller method

Sinnbeck's avatar

And you arent using ajax by any chance?

Try adding a dd before the return and see if it is hit

public function priceChanges($pcode, $sprice, $fprice, $seller)
    {
        $insertChanges = \App\Models\Robo\PriceChange::create([
            'product_code' => $pcode,
            'before_update' => $sprice,
            'rival_price' => $fprice,
            'rival_name' => $seller,
            'change_price' => "100",
            'after_update' => 0,
        ]);

      dd('hit');

        return redirect(route('batch.export.excel'));  
    }

Please or to participate in this conversation.