I am trying to export large data using Laravel Excel Queued. It works fine if i remove the headings method from the export class. If i try to include headings method it give me an exception.
FatalThrowableError: Return value of App\Exports\DataTablesExport::headings() must be of the type array, null returned in DataTablesExport.php:72
In My Controller
/**
* @var $query \Illuminate\Database\Eloquent\Builder
*/
$columns = array_keys($query->first()->toArray());
(new DataTablesExport($query, $columns))->queue($file)->chain([
new NotifyAdminOfCompletedExport(request()->user(), $file)
]);
In Export Class
<?php
namespace App\Exports;
use Illuminate\Bus\Queueable;
use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithCustomQuerySize;
class DataTablesExport implements FromQuery, WithCustomQuerySize, WithHeadings, ShouldQueue
{
use Queueable, SerializesModels, Exportable, InteractsWithQueue;
/**
* @var Builder
*/
private $query;
private $columns;
/**
* DataTablesExport constructor.
* @param Builder $query
* @param array $columns
*/
public function __construct(Builder $query, array $columns)
{
$this->query = $query;
$this->columns = $columns;
}
/**
* @inheritDoc
*/
public function query()
{
return $this->query;
}
/**
* @inheritDoc
*/
public function querySize(): int
{
return $this->query->count();
}
public function __sleep()
{
return []; //Pass the names of the variables that should be serialised here
}
public function __wakeup()
{
//Since we can't serialize the connection we need to re-open it when we unserialise
$this->connection = app(Connection::class);
}
/**
* @return array
*/
public function headings(): array
{
return $this->columns;
}
}
Queue driver: Redis
Exception message from laravel failed_jobs table (Symfony\Component\Debug\Exception\FatalThrowableError: Return value of App\Exports\DataTablesExport::headings() must be of the type array, null returned in DataTablesExport.php:72)
This implementation works if I removed the headings method from DataTableExport Class
Any suggestions to add headers to the excel file i am trying to export?