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
index.blade.ph
<li >
<a href="#" >
<span >
<i ></i>
</span>
<span @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 Illuminate\Support\Facades\Input;
use Maatwebsite\Excel\Facades\Excel;
use App\Imports\ImportUser;
use App\Exports\ExportUser;
use App\Models\User;
public function exportemv(Request $request){
return Excel::download(new ExportUser, 'users.xlsx');
}
ExportUser.php in app/Exports
<?php
namespace App\Exports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;
class ExportUser implements FromCollection
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return User::select('username','email','password')->get();
}
}
CodePudding user response:
you can download it this way
<a href="{{route('...')}}" target="_blank">Download</a>
CodePudding user response:
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 the Content-Type
.