I want to enter 100,000 records into a database through an Excel file and use field validation. Now my question is, can the output of validation errors be paginated? Because displaying 100,000 records on one page is not beautiful.
1 Answer
I think, this is about async job. This is pseudo code only for show the path.
Get file -> show user "Ok, in process" -> dispatch process job -> after processing notify user -> attach the processing result (or link to page with results)
You can put report in cache - but if you have really large excels - report in file maybe useful.
//controller action
public function store(UploadExcelRequest $request)
{
//validation etc
$file = Storage::put('excel.xls', $request->file);
//create record about this file, save patch to file
$excel = UploadedExcel::create(['path' => 'excel.xls']);
ProcessExcel::dispatch($excel);
return response()->json($excel, 201);
}
//Job for process
class ProcessExcel implements ShouldQueue
{
private ProcessExcel $excel;
public function __construct(ProcessExcel $excel)
{
$this->excel = $excel;
}
public function handle()
{
$file = Storage::get($this->excel->path);
//process your file, get $errors
$content = $errors ?? [];
Storage::put('file_report.json', json_encode($content));
$this->excel->report = 'file_report.json';
$this->excel->save();
Notification::send($this->excel->user, new ExcelProcessedNotification($this->excel));
}
}