自定义网站 Laravel Excel 排队导出序列化错误

Maatwebsite Laravel Excel queued export serialization error

我正在使用 Maatwebsite\Excel 在我的应用程序中处理数据导出。有些出口相当大,所以我想排队出口。我遵循了 documentation,但在尝试导出时出现以下错误:

You cannot serialize or unserialize PDO instances

我知道 PDO 实例不能被序列化,我只是不明白为什么它告诉我,因为我正在按照文档中的说明进行操作。这是我的代码:

控制器

$path = 'public/validations/' . $filename;
//the $client var is a record retrieved from a model, $input is the result of $request->all()
(new ValidationsExport($client, $input))->queue($path)->chain([
    // the script never reaches here, but $user is from \Auth::user()
    new NotifyUserOfValidationExport($user, $filename),
]);

导出

class ValidationsExport implements FromQuery, WithHeadings, WithMapping, WithStrictNullComparison, WithCustomQuerySize, WithEvents, WithColumnFormatting
{
    use Exportable;

    private $client;
    private $input;

    public function __construct($client, $input)
    {
        $this->client = $client;
        $this->input = $input;
    }

    public function query()
    {
        // this does return a query builder object, but this is required for FromQuery and is shown in the docs as an example of how to queue an export
        $this->validations = $this->getValidations();

        return $this->validations;
    }

    public function querySize(): int
    {
        $query = ....
        $size = $query->count();
        return $size;
    }

    public function headings(): array
    {
        // this is an array
        return $this->columns;
    }

    public function columnFormats(): array
    {
        return [
            'A' => NumberFormat::FORMAT_TEXT,
            'B' => NumberFormat::FORMAT_TEXT,
            'C' => NumberFormat::FORMAT_TEXT
        ];
    }

    public function map($row): array
    {
        $mapping = [];
        foreach($row as $key => $value) {
            if(is_bool($value)) {
                if($value) {
                    $mapping[$key] = "Yes";
                } else {
                    $mapping[$key] = "No";
                }
            }else{
                $mapping[$key] = $value;
            }
        }
        return $mapping;
    }

    //.......
}

我假设问题出在使用 FromQuery,但我不能使用 FromCollection,因为我 运行 由于导出太大而内存不足。我需要 FromQuery 使用的内置分块。有没有一种方法可以使用 FromQuery 对导出进行排队?

您可能不需要为该构建器设置成员变量 $this->validations。这就是最终试图被序列化的东西。如果您只是要 return 它,则不需要在 class.

上存储副本