web-chiru's avatar

why use patch where post is done the same job

I have used post to update any model in my project i have basic idea of other http methods like patch put delete but not sure whats the difference with get and post and not sure why use those while same thing served with pots method too. Possible then explain with example. thank you.

0 likes
6 replies
click's avatar

PATCH is normally used as a partial update. Where PUT is a full update and POST is a create.

Let's say you have a blog post with a title, body. If you only want to update the title you could send a PATCH request with only the title.

I do not implement the PATCH logic a lot because most of my endpoints update a full resource and not a part of a resource. If you use forms to update your models most of the time you do not need a PATCH value. But if you build an API and you want to be able to update 1 single field without sending the full resource you do want to implement the PATCH request.

A google search on PUT vs PATCH gives you a lot more information:

4 likes
jlrdw's avatar

This stuff has been covered in detail Before.

I usually stick with post or put.

web-chiru's avatar

@click Thanks for your reply. I understand you point but the actual question is same thing can be achieved with post method to update any model by passing, setting and updating fields. So why is patch method and not post. Why i should use patch method over post method when i want to update any model with all or few fields?

Cronix's avatar

Because it's the proper and designated use as defined in web standards. Most people like to follow the standards, because most other people follow them, so everything is, you know, standardized.

Here are the standards: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

If web development/coding in general is something you want to pursue as a career, you really should be following the standards (and know them to begin with). They exist for a reason.

jlrdw's avatar

You are going to be better off studying each of the three and decide on a case-by-case basis which to use.

If you study them well then you will know.

But I never use patch just post and put.

but the actual question is same thing can be achieved with post method to update any ...

By asking the question again, I don't understand the question simply because laravel has nothing to do with the web standards, that would be a question for that team.

You are free to never use put and Patch you can use post every time, to each his own.

Many times those other two methods are used in restful applications where in an API you have a table with quite a few fields and you can simply patch two fields without over using resources to change all 20 fields.

So I would imagine if you even having to ask such a question this advanced stuff with resources and apis probably does not apply to you.

click's avatar

@web-chiru you can also create models with a DELETE request... and also with a GET request. You can do whatever you want. But we are talking about standards here.

The benefit of using PATCH over PUT with partial updates is that your controller knows what is expected of him. If he receives a PATCH request he knows that he only need to update the fields that are coming in. All other fields can be ignored and untouched. If you send a PUT request he knows he is going to update the full model. So if you do not send a body field with a PUT request your body will be removed or your request will be denied because of a missing body or empty body field depending on your validation.

All of this logic needs to be implemented by the developer of course. There is no magic here.

Please or to participate in this conversation.