Emokores's avatar

Cannot upload/overwrite file in update() method

I have an update method that is supposed to allow a user to upload a file. When I update the fields without the file upload, the request passes a success message. However, when I try a file upload, all the validation errors are fired, despite the form having all the required data.


if ($request->hasFile('file_url')) {
      $file = $request->file('file_url');
      $name = $file->hashName();
      $path = $file->move('zips', $name);

     if (Storage::exists($task->file_url)) {
           Storage::delete($task->file_url);
     } else {
           return null;
     }
}

$task->update([
       'task_name' => $request->task_name,
       'due_date' => $request->due_date,
       'start_date' => $request->start_date,
       'priority' => $request->priority,
       'status' => $request->status,
       'file_url' => $path ?? '',
]);

I cannot understand what I'm doing wrong.

0 likes
2 replies
Sinnbeck's avatar

I don't see any validation? But why are you returning null? If the user never uploaded, it just stops everything?

Emokores's avatar

@Sinnbeck my validations are seperatetly handled in StoreTaskRequest class. At first, I was returning $task->update([$allTheVariablesHere]) but it was still doing the same thing. So I thought by returning null and continuing with the rest of the validation it would work. It actually works until the user tries to upload a file

Please or to participate in this conversation.