Export Excel File in Laravel using Inertia JS

65 Views Asked by At

Can anyone please tell me, why The exportMPR returns a 302 status code with no-response? I don't understand what's wrong with the code.

FYI: Excel Package: https://laravel-excel.com/ is used
Laravel Version: 10

Controller function

public function exportMPR()
    {
        try {
            return Excel::download(new MPRExport(request('month'), request('year')), 'mpr.xlsx');
        } catch (Exception $e) {
            dd($e);
        }
    }

Web.php

Route::middleware('auth')->group(function () {

    Route::controller(MPRController::class)->name('mpr.')->prefix('mpr')->group(function (): void {
        Route::post('export-mpr', 'exportMPR')->name('exportMPR');
    });
});

ViewMPR.vue File

const downloadMPR = async () => {
    downloadMPRForm
        .transform((data) => ({
            year: data.yearMonth.year,
            month: data.yearMonth.monthIndex
        }))
        .post('export-mpr')
}

 <!-- Download MPR Form -->
            <div class="bg-white rounded-lg shadow-lg border p-5">
                <h1 class="text-gray-800 md:text-2xl font-bold mb-2">
                    Download MPR
                </h1>
                <form @submit.prevent="downloadMPR">
                    <div class="mb-6">
                        <month-picker-input :no-default="true" @change="getMonthYear"
                            v-model="downloadMPRForm.yearMonth"></month-picker-input>
                    </div>
                    <button type="submit"
                        class="text-white bg-blue-700  focus:outline-none  font-medium rounded-lg text-sm px-5 py-2.5 "
                        :disabled="downloadMPRForm.processing" :class="{ 'opacity-25': downloadMPRForm.processing }">
                        Submit
                    </button>
                </form>
            </div>

Auth Middleware

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     */
    protected function redirectTo(Request $request): ?string
    {
        return $request->expectsJson() ? null : route('login');
    }
}

I've tried various other ways such as storing it in storage and fetching it, or dumping raw data, but it always ends up with 302 redirect. I was expecting, either direct file download or raw blob data.

0

There are 0 best solutions below