Fix Laravel’s Ajax PUT/PATCH issue

Stanley Masinde
An Idea (by Ingenious Piece)
2 min readSep 28, 2020
Photo by Chris Ried on Unsplash

Problem

You are trying to update files from your SPA and you notice that the files turn out blank. You cannot seem to figure out what’s wrong with your code and you’re about to give up and get your pilot’s license and switch careers because software development is taking a toll on your mental health.

Well, that was me before I discovered that this was a bug in PHP and not Laravel itself. Okay, let me make this clear. The normal PUT/PATCH methods work fine and by normal I mean the application/x-www-form-urlencoded data.

Trouble begins when you use the multipart/form-data. Everything works fine with POST method but nothing shows up when using PUT/PATCH method.

Solution

There are several ways to fix this some involves creating a middleware to handle multipart/form-data in PUT requests(I find this very messy). I’d recommend you use method spoofing.

Method spoofing
Method spoofing

The above method will tell Laravel to treat the request as a PUT request. Just like that, you are good to go.

--

--