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

wonder95's avatar

How to get CSV export from Vue page using Laravel Excel

I'm using the [Laravel Excel] library to generate a CSV export from my Inertia/Vue app, and I can't figure out how to navigate to the page from vue to get the export to run.

I have a pretty basic report at /admin/reports/myreport, and the Laravel Excel process working at /admin/reports/myreport/export/{start}/{end} (see here for more detaiis if interested). What I would like to do is from the report page, be able to click an Export button/link and have the file exported and downloaded via the browser. If I go directly to /admin/reports/myreport/export, the file is generated and downloaded. However, I've tried both using a callback attached to a button

<button class="px-2 py-2 bg-indigo-600 text-white rounded-md" @click="csvExport">Export</button>

    function csvExport() {
        router.get(`/admin/reports/nra/export/${start.value}/${end.value}`)
    }

and a link

<Link class="px-2 py-2 bg-indigo-600 text-white rounded-md" href="/admin/reports/nra/export">Export</Link>

but neither work. Because Laravel Excel requires the controller method to return the download call,

    public function export($start, $end)
    {
        return Excel::download(new NRAPaymentExport($start, $end), 'NRAPayments.csv');
    }

I can't return a route to another page (/export/{start}/{end} in this case) in my controller. How can I generate the csv export with the button on my report page?

0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To trigger a CSV export and download from a Vue component, you can simply create a link that points to the export URL and let the browser handle the file download. You don't need to use Vue Router for this since it's not a navigation action but a file download action.

Here's how you can modify your Vue component to include a link that triggers the CSV export:

<template>
  <div>
    <!-- ... other parts of your template ... -->

    <!-- Export button -->
    <a :href="exportUrl" class="px-2 py-2 bg-indigo-600 text-white rounded-md">Export</a>
  </div>
</template>

<script>
export default {
  data() {
    return {
      start: '2023-01-01', // Example start date
      end: '2023-01-31', // Example end date
    };
  },
  computed: {
    exportUrl() {
      return `/admin/reports/myreport/export/${this.start}/${this.end}`;
    },
  },
  // ... other parts of your script ...
};
</script>

<style>
/* Your styles here */
</style>

In this example, the exportUrl computed property dynamically generates the URL for the export link based on the start and end data properties. When the user clicks the "Export" link, the browser will navigate to the URL, and the Laravel backend will trigger the CSV download.

Make sure that your start and end values are formatted correctly and match the expected format in your Laravel route.

Also, ensure that your web server and Laravel application are configured to handle file downloads correctly, and that the Excel::download method is returning the correct response headers for a file download.

If you need to trigger the download without navigating (e.g., using an AJAX request), you would need to handle the response on the client side and create a Blob object to download the file, but this is more complex and not necessary for simple CSV exports.

1 like
asifmushtaq38's avatar

@laryai What about authentication issue? As that url will not be accessible directly in browser without Authorization header.

Please or to participate in this conversation.