Home > front end >  Laravel test file upload returns null
Laravel test file upload returns null

Time:06-15

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.

CodePudding user response:

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

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