Reputation: 178
I have this laravel project where i added this package laravel-excel
to export data from database now instead of downloading it is saving to this directory storage\framework\cache\laravel-excel
this is my vue code
<script setup>
import { router} from '@inertiajs/vue3';
import { ref } from 'vue'
const props = defineProps({transactions:Object});
const status =ref('Status');
const shop =ref('Shop');
const handleExport = ()=>{
const formData = new FormData();
formData.append('status', status.value);
formData.append('shop', shop.value);
router.post('/export_data', formData,{
preserveState: true, preserveScroll: true,
})
}
</script>
<template>
<button @click="handleExport()" type="button"></button>
</template>
here is my controller code
<?php
namespace App\Http\Controllers;
use App\Exports\TransactionsExport;
use App\Models\Transaction;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
class TransactionController extends Controller
{
public function export_data(Request $request)
{
$status = $request['status'];
$shop = $request['shop'];
$result = Transaction::where([['status',$status],['shop',$shop]])->get();
return Excel::download(new TransactionsExport($result), 'Transactions.xlsx');
}
}
additional information, am getting the right data but again instead of downloading its just saving.
thanks
Upvotes: 0
Views: 88
Reputation: 46
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');
}
Upvotes: 1