MahmoudTR's avatar

Laravel is not reading axios patch method parameters

I'm trying to upload form with products info the issue that is the backend is not reading the payload

let formData = new FormData();
formData.append('id',this.$refs.id.value);
formData.append('name',this.$refs.name.value);
formData.append('price',this.$refs.price.value);

axios.patch(this.updateUri,this.getFormData(),{
	headers: {
		"Content-Type": "multipart/form-data",
	}
})

the update method in Products Controller:

public function update(){
        return response()->json([
            'code' => 200,
            'request' => request()->all(),
        ]);
}

the response is {code: 200, request: []} & the payload :

id: 1
name: Qui vitae aut repellat aliquid quasi magnam.
price: 6.8435
0 likes
5 replies
MahmoudTR's avatar

@vincent15000 Thanks but he took a very different approach, I figure it out that the patch method doesn't work with FormData so I used JSON instead.

1 like
Sinnbeck's avatar

You need to send it as post instead and add an extra parameter named _method: 'put',. The inertia docs describe it (I know you aren't using inertia, but I recalled them explaining it) https://inertiajs.com/file-uploads last section on the page

2 likes
MahmoudTR's avatar

@Sinnbeck Thanks, I tried this in 2 ways & but went for the post route in both times:

  • First I appended the _method to the FormData as field.
  • Second I used it as a config with axios. To be fair I tried it with patch not with put so maybe it's the issue. Anyway solved it using JSON instead of FormData. Thank you again for your help.
1 like

Please or to participate in this conversation.