Reputation: 609
I am trying to download db data in excel format but when I click on download then it says:
Call to undefined method Illuminate\Database\Query\Builder::toArray()
Controller
function excel()
{
$pdf_data = DB::table('importpdfs')->toArray();
$pdf_array[] = array('Battery', 'No_of_questions_attempted', 'SAS', 'NPR', 'ST', 'GR');
foreach ($pdf_data as $pdf) {
$pdf_array[] = array(
'Battery' => $pdf->Battery,
'No_of_questions_attempted' => $pdf->No_of_questions_attempted,
'SAS' => $pdf->SAS,
'NPR' => $pdf->NPR,
'ST' => $pdf->ST,
'GR' => $pdf->GR
);
}
Excel::download('Pdf Data', function ($excel) use ($pdf_array) {
$excel->setTitle('Pdf Data');
$excel->sheet('Pdf Data', function ($sheet) use ($pdf_array) {
$sheet->fromArray($pdf_array, null, 'A1', false, false);
});
})->download('xlsx');
}
Upvotes: 0
Views: 2849
Reputation: 8252
You are trying to call toArray
on the query builder, you have to load the data first and then call toArray
on the collection:
change
$pdf_data = DB::table('importpdfs')->toArray();
to
$pdf_data = DB::table('importpdfs')->get()->toArray();
Upvotes: 2