我正在做一个项目
我做了一个可导出的课程
这是班级
- 我正在发送查询以导出类型“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;
}
}
并将其调用到我的控制器中
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
- 以其他方式对查询进行编码(IE anourvalar/eloquent-serializehttps://packagist.org/packages/anourvalar/eloquent-serialize https://packagist.org/packages/anourvalar/eloquent-serialize).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)