public function exportCabinetOrder() :JsonResponse{ $id = request('id', ''); $orderBoxDetails = CabinetOrder::with('orderDetails.inboundOrderDetails.orderDetail.details')->find($id); $orderDetails = $orderBoxDetails->订单详细信息->入站订单详细信息->订单详细信息; $details = []; foreach ($orderDetails as $orderDetail) { $details[] = $orderDetail->详细信息; } 返回 $this->resSuccess($details); } 新增导出为表格功能
时间: 2024-04-23 09:27:51 浏览: 162
JSONResponse:https:om-chauhan.github.ioJSONResponse
您可以按照以下方式修改代码以添加导出为表格的功能:
```php
use Maatwebsite\Excel\Facades\Excel;
public function exportCabinetOrder(): JsonResponse
{
$id = request('id', '');
$orderBoxDetails = CabinetOrder::with('orderDetails.inboundOrderDetails.orderDetail.details')->find($id);
$orderDetails = $orderBoxDetails->orderDetails->inboundOrderDetails->orderDetail->details;
$details = [];
foreach ($orderDetails as $orderDetail) {
$details[] = $orderDetail->details;
}
$data = $this->generateExcelData($details);
$filename = 'cabinet_order_' . $id . '.xlsx';
return Excel::download(new CabinetOrderExport($data), $filename);
}
private function generateExcelData($details)
{
$data = [];
foreach ($details as $detail) {
$data[] = [
'Column 1' => $detail->column1,
'Column 2' => $detail->column2,
// Add more columns as needed
];
}
return $data;
}
```
请确保已安装 `maatwebsite/excel` 扩展包,并创建一个 `CabinetOrderExport` 类来定义导出的 Excel 表格的格式。
请根据您的需求修改 `generateExcelData` 方法中的列名和数据字段,以便正确导出表格。
阅读全文