Uppy "PUT" XHR(multipart/form-data) request results to empty Laravel $request array

1.4k Views Asked by At

Hope you guys can help me. I'm using Uppy, with XHRUpload plugin, to upload a file but I'm getting an empty array of $request on the server side (Laravel) when I'm about to update a file using PUT request. I've tried method spoofing but still no luck. Here's my code...

I'm using uppy.setMeta by the way to add some additional data to the request.

    uppyUploader.setMeta({ 
      ...($(this).data('action') != 'store') && { _method: 'PUT' }, // Method spoofing of Laravel but the original method is 'POST'
      filename: $('input[name="filename"]').val(),
      ...
    });
    uppyUploader.upload();

but I got this in my update controller method's $request

$request->all(); // [] <-- empty array
2

There are 2 best solutions below

2
On
  • To verify if the file is available in request you can use $request->hasFile('filename')

  • To get the specific file from request you can use $request->file('filename') or $request->filename

  • To get all files from request, you can use $request->allFiles()


For more details check Laravel Docs: https://laravel.com/docs/master/requests#files

1
On

It turns out that it was just a caching issue on the server side. Just notes to remember, if you're using XHR, you can easily set the method to PUT, but when you are using traditional forms, you have to make sure that you spoof the method using @method('PUT') and set the form method to POST as HTML5 forms doesn't support PUT method. Thanks for helping me out. Happy Coding.