HTTP Request Methods

HTTP Request Methods

There are a bunch of different request methods in Laravel but your browser doesn’t support all of them so here’s how to trick it.

Initially your browser only supports “get” and “post” requests but most common frameworks (therefor Laravel) understand a few more. Let’s look at “patch” (update) and “delete”.

Now let’s say you have a form to edit the Job to continue a familiar example if you want to give that form a “patch” or “delete” request (Method=”(PATCH / DELETE)”) you will get an error. That is because as mentioned above browsers only support “get” and “post”. So let’s look how we can make that work.

Laravel has an easy way to deal with this. Basically what you do is you make it a “post” request but tell Laravel to handle it as a “patch” or “delete” request. So the browser is happy because it’s still a “post” request but it also works as you wanted. Here’s how:

PHP
<form method="POST" action="/jobs/{{ $job->id }}">
 // @csrf
    @method('PATCH')

So all you need to do is add a “@method(‘PATCH / DELETE’)” depending on what you want / need. As mentioned you can just think of “patch” as updating a resource.

Tags: