Hello I am trying to export excel file using laravel queue. I have integrated maatwebsite for the same.
public function export()
{
$this->dispatch(new ExportDistributorJob([]));
Session::flash('success','Data is being exported to excel file.');
return redirect()->back();
}
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Model\Products;
use Excel;
class ExportDistributorJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(array $request)
{
$this->queue = 'default';
$this->data = $request;
}
public function handle()
{
$data = array(
'Name' => 'John',
'City' => 'Washington'
);
return Excel::create('invoice', function($excel) use ($data) {
$excel->sheet('mySheet', function($sheet) use ($data)
{
$sheet->fromArray($data);
});
})->download('xlsx');
}
}
?>
When I execute the program. It stores data in jobs table and attempts 3 times to execute after that gets
MaxAttemptsExceededException error.
I am not able to export data to excel file using queue.
QUEUE_DRIVER=database
Any help would be appreciated.
Thanks.
The Excel
class is imported incorrectly from the package namespace.
use Maatwebsite\Excel\Excel;
Also ExportDistributorJob::handle
method is returning a HTTP response and terminates the script
In the context of a job, this is wreckless as it ends the job abruptly. You should instead store the spreadsheet .
public function handle()
{
$data = array(
'Name' => 'John',
'City' => 'Washington'
);
return Excel::create('invoice', function($excel) use ($data) {
$excel->sheet('mySheet', function($sheet) use ($data) {
$sheet->fromArray($data);
});
})->store('xlsx', storage_path('excel/exports'));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.