1

I'm trying to test a file upload, the controller always returns null for $request->file('file')

Here is my test

test('can upload file', function () {
  Storage::fake('local');

  $file = UploadedFile::fake()->image('avatar.jpg');

  $this->post(route('admin.ajax.files.store', [
     'file' => $file
  ]));

  Storage::disk('local')->assertExists($file->hashName());
});

Then in the controller:

public function store(Request $request)
{
    dd($request->file('file'));
}

outputs null

if I look at $request->file, I see

array:1 [
  "name" => "avatar.jpg"
]

I'm not sure what I'm doing wrong to test uploading a file without actually uploading a file.

2
  • Not sure if this will help? stackoverflow.com/questions/63501268/… Commented Jun 14, 2022 at 12:13
  • Thanks, Marc in this case I'm not using Livewire this is a regular Laravel controller. Commented Jun 14, 2022 at 12:21

1 Answer 1

2

You need to move the array to ->post()

$this->post(route('admin.ajax.files.store'), [
     'file' => $file
  ]);
Sign up to request clarification or add additional context in comments.

Comments

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.