79154215

Date: 2024-11-04 04:49:43
Score: 0.5
Natty:
Report link

It looks like the response generated by Excel::download is not being handled correctly on the frontend, which is causing it to be saved to the server's cache instead of prompting a file download on the client.

Here’s how to solve it:

Use Inertia::post instead of router.post for download: Since you want the file to download directly and not go through Inertia’s SPA-like behavior, a typical Inertia post request will not work as expected. Instead, make a direct GET request from the client.

Update the Vue Code to Trigger a GET Request: Change the handleExport function to open the download link directly in the browser using window.location.

Revised Code Vue Component (handleExport function) Update the handleExport function to construct a URL with query parameters and initiate a download request:

import { ref } from 'vue';
const props = defineProps({ transactions: Object });

const status = ref('Status');
const shop = ref('Shop');

const handleExport = () => {
    // Construct a query string for GET request with parameters
    const params = new URLSearchParams({
        status: status.value,
        shop: shop.value
    }).toString();

    // Redirect to the URL which triggers the download
    window.location.href = `/export_data?${params}`;
};


<template>
    <button @click="handleExport" type="button">Export Data</button>
</template>

Controller (export_data function) Change the export_data method to accept the parameters as a GET request:

    public function export_data(Request $request)
{
    $status = $request->query('status');  // Use query parameters
    $shop = $request->query('shop');

    // Retrieve the filtered data
    $result = Transaction::where([['status', $status], ['shop', $shop]])->get();

    // Export and download the data
    return Excel::download(new TransactionsExport($result), 'Transactions.xlsx');
}
Reasons:
  • Blacklisted phrase (1): how to solve
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Amir Eghbal