我正在做一个项目 我做了一个可导出的类(class) 这是类(class)
- 我正在向导出类型“Illuminate\Database\Eloquent\Builder”发送查询
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMapping;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\WithHeadings;
class ExcelExport implements FromQuery, WithMapping, WithHeadings
{
use Exportable;
private $query;
public function __construct($query)
{
$this->query = $query;
}
public function query()
{
return $this->query;
}
public function headings():array
{
$return = [
'H1',
'H2',
];
return $return;
}
public function map($row):array
{
$return = [];
foreach ($row->relation as $rel) {
$return[] = $rel->column;
}
return $return;
}
}
并将其调用到我的 Controller 中
public function export(Request $request, string $type = 'excel')
{
// returns Builder
$query = $this->service->findByReportable($request, 1);
$file = new ExcelExportLead($query);
if ($file){
switch (strtolower($type)) {
case 'pdf':
$file_name = 'export-' . time() . '.pdf';
// OriginExcel refers to 'Maatwebsite\Excel\Excel'
return $file::queue($file, $file_name, OriginExcel::DOMPDF);
break;
default:
$file_name = 'export-' . time() . '.xls';
return $file->queue($file_name);
break;
}
} else{
return back()->withErrors(__('common.Sorry But there Was an issue in exporting Data please try again'));
}
但是出现的错误是“不允许序列化'Doctrine\DBAL\Driver\PDOConnection'”
我不知道如何解决它,我使用了SerializesModels
,但它没有解决问题
最佳答案
序列化是一种工具,您可以将变量/对象编码为文本(或二进制)表示形式,然后将该变量移动到另一个线程/进程。
根本问题是 Builder 对象具有 PDOConnection 属性(与数据库的连接),并且根据定义,这些对象无法序列化,因为它们通常是操作系统中无法移动的文件描述符到另一个进程/线程。
解决方案应该遵循以下任一路线
- 使用 builder->toSql() 方法将查询作为字符串发送到 ExcelExport
- 以其他方式对查询进行编码(即 anourvalar/eloquent-serialize https://packagist.org/packages/anourvalar/eloquent-serialize )。
关于php - 不允许序列化 'Doctrine\DBAL\Driver\PDOConnection',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66420024/