vincent15000's avatar

Form request with an uploaded file

Hello,

I send a file from VueJS to Laravel via axios.

async update(branch) {
    const res = await api.put('branches/'+branch.id, { ...branch }, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    });

    useFlashStore().addMessage('success', 'branch_updated');

    return res;
},

My BranchFormRequest returns an error saying that the name is required, whereas the name is present in the payload.

Just to check the request I have replace BranchFormRequest by Request and I see in the network development tools that the request contains the name and the file.

What is wrong in my code ?

Thanks for your help.

V

0 likes
12 replies
tykus's avatar

If you are uploading a file from your Vue application, you can use the FormData browser API, for example:

async update(branch) {
    let data = new FormData();
    data.append('name', branch.name);
    data.append('avatar', this.$refs.fileInput.files[0]); // directly from the form input however it is referenced/named in your application

    const res = await api.put('branches/'+branch.id, data, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    }).then(() => useFlashStore().addMessage('success', 'branch_updated'));

    return res;
},
1 like
vincent15000's avatar

@tykus Thank you for your help.

With and without FormData, I get the same error.

When I checked the request, I said in my post that it contains the name and the file. But I think that I have checked at the wrong place in the code.

My request is empty when I specify the header with multipart/form-data.

vincent15000's avatar

@tykus Hmmm ... well something strange.

Here is the payload.

id: 1
name: Branche 1
imageToUpload: (binaire)

And here is my controller.

public function update(Request $request, Branch $branch) // Request just to check, instead it's BranchRequest
{
    Gate::authorize('update', $branch);

    return $request->all();

    $branch->update($request->all()); // just for testing why it fails

    if ($request->hasFile('imageToUpload')) {
        $branch->addMediaFromRequest('imageToUpload')->toMediaCollection('image');
    }

    return response()->json($branch, 200);
}

$request->all() is empty.

That's strange, isn't it ?

vincent15000's avatar

@tykus

Here is the y image uploader component.

And here is my service.

async update(branch) {
    const res = await api.put('branches/'+branch.id, { ...branch }, {
        headers: {
            'Content-Type': 'multipart/form-data',
        },
    });

    useFlashStore().addMessage('success', 'branch_updated');

    return res;
},

With api being.

const api = axios.create({
    baseURL: url('api/v1/'),
});

I don't use the form markup to send the data, I retrieve the data via the branch reactive property to send them to the backend.

tykus's avatar
tykus
Best Answer
Level 104

@vincent15000 that hurt my brian more than it should've - I guess I need more coffee!!! Adding the _method field in the form, and to sending a post Request will fix the issue:

    const res = await api.post('branches/'+branch.id, { ...branch, _method: "PUT" }, {
        headers: {
            'Content-Type': 'multipart/form-data',
        },
    });
2 likes
vincent15000's avatar

@tykus I wouldn't have thought about this solution ... effectively it fixes the issue.

Why do I need to do so ?

What is the problem with axios put method ?

tykus's avatar

@vincent15000 Laravel doesnt understand it - it expects a _method whenever the verb is not GET or POST and axios does not append that.

1 like
vincent15000's avatar

@tykus Ok ... and why does axios.put work without _method when I don't have multipart/form-data ?

tykus's avatar

@vincent15000 can't imagine it does... are you getting the UploadedFile in the Request?

1 like
vincent15000's avatar

@tykus With axios.put and without _method, the request is empty (no fields, no files, ...) if I have the mutlipart/form-data header, but it works with your solution.

And then sure I have the uploaded file in the request (but only with your solution).

Thank you very much, great help ;).

Please or to participate in this conversation.