There's no PUT/PATCH/DELETE method, or how to build a Laravel form manually

Laravel RESTful controller mechanism is a very convenient thing - we generate Controller, write {{ Form::xxxx() }} methods in Blade templates and it works like magic. But there's a little trick there which I want to talk about - it's PUT/PATCH/DELETE methods for updating the database entries.

Let's remember the theory first - here's how Laravel official documentation explains different methods for RESTful controllers:

0707_laravel_restful

If you are not using Laravel and want to build a form manually, you cannot use PUT/PATCH - there's just no such methods supported by forms in browsers - it's only GET and POST. So how does Laravel make it work with {{ Form::create(['method' => 'PUT']) }}?

Actually, under the hood the generated HTML looks like this:

<form accept-charset="UTF-8" action="" method="POST"><input name="_method" type="hidden" value="PUT"></form>

That's right, Laravel constructs a hidden field with name _method and then checks it upon form submittion, routing it to the correct Controller method.

So if for any reason you would need to build the FORM tag yourself, don't put <form method="put"> (same applied to patch and delete) - it just won't work. Instead add hidden fields, if necessary.

No comments yet…

Like our articles?

Become a Premium Member for $129/year or $29/month

Written by

You might also like