Hello everyone I have a queued Laravel job that runs to insert some records in the database for an imported file by a user. but whenever I access the request object to get the uploaded file from inside the job I got null. however, the file is received in the controller normally. any idea?
find the import method below in the controller
public function import(ImportPointRequest $request)
{
Storage::makeDirectory('import_logs');
$file = request()->file('file');
$fileName = 'importlog_date_' . date('Y-m-d') . '-user_' . auth()->id() . '.xlsx';
$logFile = 'import_logs/' . $fileName;
$readerTypes = ['xlsx' => Excel::XLSX, 'xls' => Excel::XLS, 'csv' => Excel::CSV];
$link = route('file.show', ['import_logs', $fileName]);
try {
ExcelFacade::import(new PointsImportHeading(), $file);
} catch (\Exception $e) {
return $this->returnBadRequest(config('point.error-codes.import-fail'), $e->getMessage());
}
(new PointsImport(auth()->user(), auth()->user()->account_id, $logFile))->queue(
$file->getRealPath(),
null,
$readerTypes[request()->file('file')->getClientOriginalExtension()]
)->chain([new AfterImportJob(auth()->id(), $logFile, $link)]);
return $this->returnSuccess(trans('point::point.import-queued', ['module' => trans('point::point.point')]));
}
find the getImportedFileContent method below in the job
protected function getUploadedFileContent(): array
{
return Excel::toArray(new PointsImportHeading(), request()->file('file'));
}
The problem in this part request()->file('file') always returns null.
request()will be empty. This is done to speed up the response to the user and execute logic away from the request. You need to store that file somewhere and later read from it inside of your job. Read more about Queues in the Docs