0

The information which I got by my previous question Vuex + Laravel. Why axios sends any values but only not that one, which come's with method's parameter? works only for situation when I pass only one argument in axios. I also found how to successfully pass multiple files with one argument, but in my situation, I have to use object, to be able to pass more than one argument. So instead of

const response = await axios.post('/posts', data)

I have to use the next one

const response = await axios.post('/posts', {
                formData:data,
                body:postBody
            })

I thought it would be easy, but now is the 5-th hour that I can't find the solution.

enter image description here

controller

public function store(Request $request)
    {
        $files = $request->all();
        return var_dump($files);
    }

I tried many things, included $request->allFiles() and getClientOriginalExtension() experiments. No results. What versions do you have? How this problem can be fixed?

Important! I use Laravel, vuex, axios.
I use text not in FormData cause it's textArea. And if I can combinate textarea in FormData to make one argument axios call, anyway it still very important to me to know what's the problem which I described above.

Updated

Here's chrome's payload tab

enter image description here

Ruben Kubalyan
  • 288
  • 3
  • 11
  • Are you completely sure that files are sent to the endpoint? Could you share your request payload from chrome's dev console? – Luciano Dec 04 '21 at 00:46
  • @Luciano no. It's just very strange. Without object method as a single argument everything works, but as object — Nothing. I updated the question. You can watch the screenshot you wanted. – Ruben Kubalyan Dec 04 '21 at 08:38
  • Why don't you just send everything within formData object? Why would you want to send a formData and also data? – Luciano Dec 04 '21 at 15:32
  • @Luciano as I see, it's not logically correct to send all data with only one FormData. Cause formData means data, which belongs to form context. Textarea maybe we can take as a form data, but what if I want to send another data too? – Ruben Kubalyan Dec 04 '21 at 16:10
  • 1
    Well, the short answer is "you can't". In terms of "logically correct" why would you send a `post` payload with some data that do not regard the post-action itself? – Luciano Dec 04 '21 at 16:27

1 Answers1

1

It's not pretty clear why you don't send everything you need within formData object.

You could do something like:

const formData = new FormData()

formData.append(body, postBody)
formData.append(files, fileList)
formData.append(otherData, someOtherData)

const response = await axios.post('/posts', formData)

Then in your server side you will be able to do

 $body = $request->body;
 $files = $request->files;
 $otherData = $request->otherData;
Luciano
  • 2,052
  • 1
  • 16
  • 26