6
votes

For defining a route as a resource with Route::resource, the docs indicate:

Verb        Path                            Action  Route Name
-------------------------------------------------------------------
GET         /resource                       index   resource.index
GET         /resource/create                create  resource.create
POST        /resource                       store   resource.store
GET         /resource/{resource}            show    resource.show
GET         /resource/{resource}/edit       edit    resource.edit
PUT/PATCH   /resource/{resource}            update  resource.update
DELETE      /resource/{resource}            destroy resource.destroy

as per typical REST CRUD so PUT/PATCH is used for update actions and DELETE for destroy actions. I would think that is meant to define a typical resource interaction, even when manually defining my own routes and controller actions.

Here's the core of what I understand about these interactions with Laravel:

Are PUT and DELETE solely there to create externally-accessible json REST APIs or do they serve another purpose? Is there some benefit aside from routing to the same URI with a different endpoint, enforcing the presence of a _method in $_POST or the json?

PUT and DELETE are supposed to be idempotent, but is this even implemented in Laravel? Is this something that I have to make happen in my controllers or does the routing enforce this somehow?

Essentially, if PUT and DELETE in Laravel are functionally identical to POST, aside from REST semantics and parallel routing, when and why should I use them over POST?

2
Answer is contained in your question - you should use PUT and DELETE when dealing with RESTful actions, if you require those verbs. As with everything - you don't really have to do it.N.B.
I tried to make the question clear by including a complete description of the scenario, but you're right that it was leading. Your answer is that yes, they are there solely for external REST APIs and that they hold no benefit aside from conforming to the standard verbs of the REST interaction model? No idempotence or other meaning? Internally, I could use POST and distinct URIs to the same function. How disappointing. Any source on the lack for idempotence? If you could make it an answer, I could accept it.skovacs1
Short answer: clarity. If you're stepping in to a new project and looking through Routes to see what's happening and you see a route::post, all you really know is that something is getting posted to that route. If you see route::delete, you know that at the end of that action, something should be deleted. One of Laravel's overarching principles is to ensure code is expressive.James Binford
While HTML forms only support GET and POST, almost all AJAX implementations support PUT and DELETE. stackoverflow.com/questions/8785248/…Collin James
@JamesBinford That's what I mean by "REST semantics" and "conforming to the standard verbs of the REST interaction model." It's all semantic. If routes have descriptive URIs and/or map to descriptive controller actions, then it should be clear as well. It is certainly expressive that it will enforce the presence of a _method with those verbs, but when it lacks the function behind the separation (in this case idempotency), it really becomes purely semantic akin to many html5 tags that simply resolve to <div>s in the end and only bloat the source.skovacs1

2 Answers

2
votes

You use PUT method when you want to Update a record, And you use DELETE method when you want to Delete a record.

Note that in the resourceful controller, both PUT and DELETE method directed to the same url (resource/{resource}), so if you don't distinguish the method with PUT or DELETE, it will be a problem.

1
votes

GET: To get data,

POST: To submit data,

PUT: To update data,

DELETE: To destroy data