0

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.

2
  • 3
    Jobs are dispatched to a queue for the server to run away from the request this is why 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 Commented Jun 21, 2023 at 16:28
  • Yes that's absolutely right thanks for sharing that info Commented Jun 21, 2023 at 17:00

1 Answer 1

2

Your approach is not true. In Laravel, the request lifecycle is started when a request comes to your server and it ends as soon as a response is sent back to the user's browser. When you queue a job in Laravel, it means that job will be processed later, perhaps even on a different server. When the job actually runs, the original request lifecycle is already over. Therefore, you cannot access the request data inside your queued job.

If you need to use the uploaded file in your queued job, you'll need to store the uploaded file in a location where your job can access it. This could be on your server's file system or on a cloud storage service.

In your controller, you're already storing the file temporarily to process it with Excel:

$file = request()->file('file');

However, you don't persist the file, which is why the file is not available when the job runs. You need to store the file somewhere more permanently.

After store your file permanently you can read the file from the new location.

Sign up to request clarification or add additional context in comments.

1 Comment

You're absolutely right thanks for the explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.