drunkOctopus
drunkOctopus

Reputation: 41

Maatwebsite excel Serialization of 'PDO' is not allowed

Im tring to export large data in queue on s3 and getting Serialization of 'PDO' is not allowed exception/ Here is my code:

Controller

            $transactions = Transaction::query()
        ->with([
            'user',
            'user.profile',
            'senderUser',
            'receiverUser',
            'senderUser.roles',
            'receiverUser.roles'
        ])->filterByUser()
        ->filter($filters)
        ->orderByDesc('created_at');

    if (request()->export_transactions){
        (new TransactionsExport(auth('sanctum')->user(),$transactions))->store('transactions-exports/' . now()->format('d:m:Y') . '.csv', 's3', \Maatwebsite\Excel\Excel::CSV);

        return response()->json('Export started');
    }

Export file

    class TransactionsExport implements FromQuery, WithMapping, WithHeadings, WithCustomQuerySize, ShouldQueue
{
    use Exportable;

    private $user;
    private $transactions;

    public function __construct(User $user, $transactions)
    {
        $this->user = $user;
        $this->transactions = $transactions;
    }

    /**
    * @return Builder
    */
    public function query()
    {
        return $this->transactions;
    }

    public function querySize(): int
    {
        return $this->transactions->count();
    }


    public function headings(): array
    {
        return [
          //headings
        ];
    }
    public function prepareRows($transactions): array
    {
        //code here
    }

    public function map($transaction): array
    {
        //code here
     
    }
}

I also tried to trigger it like this (with additional paramethers and without and with different allowed methods (store, download etc.))

(new TransactionsExport(auth('sanctum')->user(),$transactions))->queue('transactions-exports/' . now()->format('d:m:Y') . '.csv', 's3', \Maatwebsite\Excel\Excel::CSV);

Also, I tried move Transaction::query() direct to query method in export file. Also didnt help toSql() method (Call to a member function count() on string exception appears)

Don't know what I'm doing wrong. Thanks for help.

Upvotes: 2

Views: 1200

Answers (1)

drunkOctopus
drunkOctopus

Reputation: 41

I have solved the problem by deleting the injection of classes in the constructor, it's not allowed in jobs (queues/ if you need an object of a class, better to call it like new Class()), and now all is working fine.

Upvotes: 2

Related Questions