Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the value of using PUT/DELETE with Laravel?

Tags:

php

laravel-4

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:

  • You can define a route to accept PUT with Route::put and DELETE with Route::delete.
  • The Input facade uses Illuminate\Http\Request::input typifying interactions and will return json_decode of content for json, all GET parameters for GET requests or all POST parameters otherwise. We must use json or not actually use PUT or DELETE at all (fine because http PUT and DELETE requests are not universally supported).
  • Laravel's own form helpers use POST for PUT and DELETE requests, including a _method in the data so that the routing can enforce the verbs.

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?

like image 479
skovacs1 Avatar asked Dec 19 '13 08:12

skovacs1


People also ask

What is use of put in laravel?

As a standard practice of creating an CRUD application there are certain actions in like update and delete which requires the method submitted to the server url to be either PUT / PATCH (to modify the resource) and DELETE (for deleting the resource). This is how the standard calls look like in Laravel CRUD application.

What is Delete method in laravel?

Laravel - Delete Records We can delete the record using the DB facade with the delete method. The syntax of delete method is shown in the following table. Run a delete statement against the database.

How do you delete a product in laravel?

\nSo switch to the browser and refresh the page. \nNow lets delete any product. \nSo just click on delete link. \nAnd you can see here the message product has been deleted.


2 Answers

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.

like image 180
GeLiNa Avatar answered Oct 09 '22 19:10

GeLiNa


GET: To get data,

POST: To submit data,

PUT: To update data,

DELETE: To destroy data

like image 26
Parth Patel Avatar answered Oct 09 '22 18:10

Parth Patel