I have a problem in which I cannot trace the error/problem when trying to export and download my Users
table in excel using Maatwebsite
I’ve followed step by step with this reference here link but it didn’t export the data through excel,
Is there any wrong with my implementation? Is somebody have an idea what’s the problem? I appreciate any comments
index.blade.ph
<li class="navi-item">
<a href="#" class="navi-link">
<span class="navi-icon">
<i class="la la-file-excel-o"></i>
</span>
<span class="navi-text" @click="exportData()">Excel</span>
</a>
</li>
Index.js
methods: {
init() {
var vm = this;
var t;
$(document).ready(function() {
vm.$toaster.init();
vm.setConfig();
});
},
exportData(){
let vm = this;
$.ajax({
url: vm.$route('staff.ajax.emvvalidationdetails.exportemv'),
type: 'GET',
success: function (response) {
if (response) {
// console.log("Test");
}
},
});
},
Staff ajax
Route::group(['prefix' => 'emvvalidationdetails', 'namespace' => 'Emvvalidationdetails'], function () {
Route::get('exportemv', 'EmvvalidationdetailsController@exportemv')->name('staff.ajax.emvvalidationdetails.exportemv');
});
EmvvalidationdetailsController
use IlluminateSupportFacadesInput;
use MaatwebsiteExcelFacadesExcel;
use AppImportsImportUser;
use AppExportsExportUser;
use AppModelsUser;
public function exportemv(Request $request){
return Excel::download(new ExportUser, 'users.xlsx');
}
ExportUser.php in app/Exports
<?php
namespace AppExports;
use AppModelsUser;
use MaatwebsiteExcelConcernsFromCollection;
class ExportUser implements FromCollection
{
/**
* @return IlluminateSupportCollection
*/
public function collection()
{
return User::select('username','email','password')->get();
}
}
2
Answers
you can download it this way
<a href="{{route('...')}}" target="_blank">Download</a>
My guess is that the response’s
Content-Type
is not being set correctly. Maybe it gets messed up by some package or other part of your codes. See the doc here to manually set theContent-Type
.