我正在尝试以excel格式下载数据库数据,但是当我单击下载时,它说:
调用未定义的方法Illuminate \ Database \ Query \ Builder :: toArray()
控制器
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');
}
答案 0 :(得分:1)
您尝试在查询构建器上调用toArray
,必须先加载数据,然后在集合上调用toArray
:
更改
$pdf_data = DB::table('importpdfs')->toArray();
到
$pdf_data = DB::table('importpdfs')->get()->toArray();