Mysql 以下CSV文件脚本在Laravel 6中无法正常工作
我在我的rout文件中使用了以下脚本Mysql 以下CSV文件脚本在Laravel 6中无法正常工作,mysql,laravel,eloquent,Mysql,Laravel,Eloquent,我在我的rout文件中使用了以下脚本 Route::get('/tasks', 'appointmentController@exportCsv') 我在blade模板中使用了以下Js脚本Html可点击按钮 <script> function exportTasks(_this) { let _url = $(_this).data('href'); window.location.href = _url; } </script>
Route::get('/tasks', 'appointmentController@exportCsv')
我在blade模板中使用了以下Js脚本Html可点击按钮
<script>
function exportTasks(_this) {
let _url = $(_this).data('href');
window.location.href = _url;
}
</script>
<span data-href="./tasks" id="export" class="btn btn-success btn-sm" onclick="exportTasks(event.target);">Export Client Email</span>
剧本都很好。当你打开网页
http:///tasks
?这可能是因为您的$task
没有所需的分配
,因此它在$task->assign->email
行中断。单击按钮时,我没有收到任何信息。真的,我不明白我的问题出在哪里。没有足够的信息告诉罪犯在哪里。尝试查看laravel.log
文件,如果没有帮助,请监视web服务器的日志
public function exportCsv(Request $request)
{
$fileName = 'tasks.csv';
$tasks = Clientinformation::all();
$headers = array(
"Content-type" => "text/csv",
"Content-Disposition" => "attachment; filename=$fileName",
"Pragma" => "no-cache",
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
"Expires" => "0"
);
$columns = array('customerName', 'email', 'cell1', 'cell2', 'landNumber');
$callback = function() use($tasks, $columns) {
$file = fopen('php://output', 'w');
fputcsv($file, $columns);
foreach ($tasks as $task) {
$row['customerName'] = $task->customerName;
$row['email'] = $task->assign->email;
$row['cell1'] = $task->cell1;
$row['cell2'] = $task->cell2;
$row['landNumber'] = $task->landNumber;
fputcsv($file, array($row['customerName'], $row['email'], $row['cell1'], $row['cell2'], $row['cell2'], $row['landNumber'] ));
}
fclose($file);
};
return response()->stream($callback, 200, $headers);
}