Search code examples
javascriptlaravelrequestaxiosform-data

Why PUT request with FormData object appends not sent to Laravel controller?


Why I can't sent FormData object values to my Laravel application

Javascript code:

console.log("Sending start...")
for (var value of company.values()) {
    console.log(value); 
}
this.$axios.put(url, company)
    .then(res => {
        console.log(res)
    })
    .catch(error => {
        console.log(error)
    })

Response preview:

enter image description here

Laravel controller:

public function update(Request $request, Company $company)
{
    return response()->json($request->all());
}

Where I've any error?


Solution

  • Try to trick the Laravel framework by sending a POST request with axios assigning method type as PUT to FormData object.

    Code:

    // Lets create FormData object
    let data = new FormData()
    data.append('_method', 'PUT')
    // ...........................
    // other your appends here...
    
    // Axios request
    this.$axios.post(url, data)
      .then(res => {
          console.log(res)
      })
    

    After checking, let me know about the result of the code :)