在您的 Vue 组件中,在将请求发送到后端之前,检查 this.bookings 是否包含数据(不为空或 null)。 在 Laravel 控制器中,在继续导出之前验证解码的数据以确保它是一个数组。 针对数据为空、null 或不符合预期格式的情况实施错误处理。 通过添加这些检查,您可以确保优雅地处理空数据或无效数据,并将数据成功导出到 Excel。
if (this.bookings.length > 0) {
// Only send the request if bookings is not empty
// Send the request here
} else {
// Handle the case where bookings is empty or null
alert('No data to export.');
}
public function export(Request $request)
{
$dataParam = $request->query('data');
if (!empty($dataParam)) {
$data = json_decode(urldecode($dataParam), true);
if (is_array($data)) {
return Excel::store(new BookingStatusExport($data), 'booking-status-report.xlsx');
} else {
return response()->json(['error' => 'Invalid data format'], 400);
}
} else {
return response()->json(['error' => 'Data parameter is missing'], 400);
}
}