LoginSignup
5
4

More than 3 years have passed since last update.

Laravelのpatch/putでハマった話

Last updated at Posted at 2019-06-13

トラブル発生

laravelで書かれたAPIの動作検証をpostmanで実行していたのですが、
patchメソッドやputメソッドではリクエストの中身が取得できていない問題が発生しました。

調査

dd($request->all());

を実行すると結果はnull

phpUnitからのテストは通っているので
サーバ側のログを追ってみると
実際のリクエストはpostで飛んできていて
_methodsというパラメータにpatchが指定されていました。

解決

postmanのフォームパラーメータのキーに
_methodを追加
値に patchやputを入れてやると上手く動きました。

axiosはpatchメソッド実行時にペイロードがjsonの場合は問題ないのですが、
ペイロードがFormDataの場合はダメという挙動でしたが
同様にformに_merthodパラメータをappendすることで対応できました。

form.append('_method', 'patch')
await axios.post(apiUrl, form)

おまけ

ちなみに、formをbladeで書く場合は

 {{ method_field('PATCH') }}

または

@method('PATCH')

をformタグの内部に書く必要があるそうです。
csrfトークンと同じですね。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4