Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vandyczech's avatar

PATCH vs. POST

Hi I've trivial question, what can't I use the POST method in my form when I tried save a new article to the database with this form:

    <form method='PATCH' action='articles' accept-charset='UTF-8'>
    <input name='_token' type='hidden' value='OlTER5KJfgVoJ6CLKI2EAsx6rYhmUWAuBOlXYgWO'>

In php artisan route:list is that articles.store have URI and method:

articles , method : POST, PATCH is only for update 

So why I can't use POST for sending form? :( I'm confused

When I use POST, I see:

    MethodNotAllowedHttpException in RouteCollection.php line 201:
0 likes
22 replies
JeffreyWay's avatar

Because most browsers don't natively recognize "PATCH" request types. We have to fake it with the framework.

<form action="/articles/1">
    <input type="hidden" name="_method" value="PATCH">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

This form will correctly trigger a patch request to `/articles/1'.

5 likes
Penderis's avatar

@vandyczech like @JeffreyWay said it is because the browser only understands "get" and "post" , so the framework does the effort of implementing the patch (update) method for you, by posting all information to php but only handling the fields actually present in the form, so you are doing a post but with a patch trigger that if present instead of requiring all 40 fields maybe of a form it knows to only update fields that were received and ignore what is not in the form.

1 like
vandyczech's avatar

But this code doesn't work:

<form method='PATCH' action='articles' accept-charset='UTF-8'>
<input type='hidden' name='_token' value='OlTER5KJfgVoJ6CLKI2EAsx6rYhmUWAuBOlXYgWO'>
<input type='hidden' name='_method' value='PUT'>
<div class='form-group'><label for='Titulek:'>Titulek:</label><input class='form-control' name='article_title' type='text'>
 <strong>Kategorie:</strong><select name='article_category' class='form-control'><option value='1'>programování</option><option value='2'>grafika</option><option value='3'>design</option></select>
<strong>Obsah:</strong><br><textarea class='col-md-12 content' rows='20' name='article_content'></textarea>
<button type='submit' class='btn btn-success'><i class='fa fa-check'></i> Přidat nový článek</button>
</form>

I want send this form to the store method in ArticlesController.php

Penderis's avatar

in your form method remove method='PATCH' , I think. it is the one that is not recognized, if you remove it laravel should default to post but see the hidden value as 'PUT' or 'PATCH'

bashy's avatar

Browers only support GET and POST. To send your request to the store method, you need to use a faked method via the POST one. The framework reads the _method value and then directs it to the correct route.

Please re-read the above comments as they told you this...

sid405's avatar

@vandyczech , like @JeffreyWay said

for PATCH requests do this

<form method='POST' action='articles' accept-charset='UTF-8'>
<input type='hidden' name='_token' value='OlTER5KJfgVoJ6CLKI2EAsx6rYhmUWAuBOlXYgWO'>
<input type='hidden' name='_method' value='PATCH'>

for PUT requests do this other one

<form method='POST' action='articles' accept-charset='UTF-8'>
<input type='hidden' name='_token' value='OlTER5KJfgVoJ6CLKI2EAsx6rYhmUWAuBOlXYgWO'>
<input type='hidden' name='_method' value='PUT'>

for DELETE requests do this other one

<form method='POST' action='articles' accept-charset='UTF-8'>
<input type='hidden' name='_token' value='OlTER5KJfgVoJ6CLKI2EAsx6rYhmUWAuBOlXYgWO'>
<input type='hidden' name='_method' value='DELETE'>

This is to make sure that even on dated browsers you laravel forms will work fine.

2 likes
vandyczech's avatar

Thank you, but where to directs. I want see the requested data, but no possible from method store() or update(). Where route the form with this hidden method PATCH?

vandyczech's avatar

Why i cant store data logiccaly with method store but form directs to update method?

jakeryansmith's avatar

If you want to store a new record use POST if you want to update use PUT. PUT and PATCH are pretty much the same thing as far as Laravel is concerned. So just use PUT for update and POST for creating a new record and forget about using PATCH.

If you are using a Route::resource Laravel will direct any PATCH requests to PUT which is connected to the update method.

Penderis's avatar

This is going to depend on whether you are using restful controllers, I speak from 4.2 but doubt it has changed much. else you would have to handle the request yourself but it is easily accomplished by just saying route::put, route::delete ect. as the second parameter you pass the uses=>Controllername@action flag (as an associative array).

Route::get('user/profile', [
    'as' => 'profile', 'uses' => 'UserController@showProfile'
]);

from documentation on named routes. So you will always be sending to the articles route but depending on the hidden method it will be handled by a different method in your controller.

vandyczech's avatar

I want use POST but when i use method post, i will see error method not allowed. I use restful route resource and update have to be for update and store for store new for example article or not? In php artisan route:list is that for store use method post but without hidden method patch is not work correctly

sid405's avatar

@vandyczech I frankly prefer to define myself the routes opposed to let the resource do it. Anyhow.

The bottom line is this:

for PATCH requests do this

<form method='POST' action='articles' accept-charset='UTF-8'>
<input type='hidden' name='_token' value='OlTER5KJfgVoJ6CLKI2EAsx6rYhmUWAuBOlXYgWO'>
<input type='hidden' name='_method' value='PATCH'>
for PUT requests do this other one
<form method='POST' action='articles' accept-charset='UTF-8'>
<input type='hidden' name='_token' value='OlTER5KJfgVoJ6CLKI2EAsx6rYhmUWAuBOlXYgWO'>
<input type='hidden' name='_method' value='PUT'>
for DELETE requests do this other one
<form method='POST' action='articles' accept-charset='UTF-8'>
<input type='hidden' name='_token' value='OlTER5KJfgVoJ6CLKI2EAsx6rYhmUWAuBOlXYgWO'>
<input type='hidden' name='_method' value='DELETE'>

This is basically all the needed information. I don't know how else to help.

2 likes
jakeryansmith's avatar
Level 8

@vandyczech Keep in mind Laravel does not have a separate route for PATCH. It will match PATCH to the update method in your controller if using resource routing.

2 likes
vandyczech's avatar

Ok, but what we need store method when restful use update? Its not logic create new article in update method, change exist articlr with update is ok, but create new should be store

sid405's avatar

@vandyczech I don't know what you are seeing in your routes list but you should be seeing this

+--------+----------+------------------+--------------+---------------------------------------------+------------+
| Domain | Method   | URI              | Name         | Action                                      | Middleware |
+--------+----------+------------------+--------------+---------------------------------------------+------------+
|        | GET|HEAD | test             | test.index   | App\Http\Controllers\TestController@index   |            |
|        | POST     | test             | test.store   | App\Http\Controllers\TestController@store   |            |
|        | GET|HEAD | test/create      | test.create  | App\Http\Controllers\TestController@create  |            |
|        | DELETE   | test/{test}      | test.destroy | App\Http\Controllers\TestController@destroy |            |
|        | PATCH    | test/{test}      |              | App\Http\Controllers\TestController@update  |            |
|        | GET|HEAD | test/{test}      | test.show    | App\Http\Controllers\TestController@show    |            |
|        | PUT      | test/{test}      | test.update  | App\Http\Controllers\TestController@update  |            |
|        | GET|HEAD | test/{test}/edit | test.edit    | App\Http\Controllers\TestController@edit    |            |
+--------+----------+------------------+--------------+---------------------------------------------+------------+
  • index - Lists all article - use GET
  • create - shows form to create a article - use GET
  • store - saves the article into the database - use POST
  • show - displays a article - use GET
  • edit - shows form to edit a article - use GET
  • update - stores modified article into the database - use PATCH or PUT - both work
  • destroy - deletes a article - use DELETE

This is all the help i can give you mate. Maybe we are just not understanding each other.

Really : http://laravel.com/docs/5.0/controllers#restful-resource-controllers

3 likes
jakeryansmith's avatar

@vandyczech An easy way to see which routes do what use the artisan command: route:list

As you will see the store method is used for creating a new record. POST is mapped to the store method with route resources.

Resource routes are set up to map the HTTP verb to these controller methods:

POST -> store method PUT -> update method DELETE -> destroy method

1 like
vandyczech's avatar

Yes, but ať the moment im on mobile and i cant see the vote icons on my phone. Tomorrow I will do it of course.

Please or to participate in this conversation.