0
0
LaravelHow-ToBeginner · 3 min read

How to Use @method in Blade Templates in Laravel

In Laravel Blade templates, use the @method directive inside a form to spoof HTTP verbs like PUT, PATCH, or DELETE since HTML forms only support GET and POST. Place @method('PUT') or any other HTTP verb inside your form to tell Laravel which HTTP method to use.
📐

Syntax

The @method directive is used inside a Blade form to specify the HTTP method you want to spoof. It accepts a single string argument representing the HTTP verb.

  • @method('PUT') spoofs a PUT request.
  • @method('DELETE') spoofs a DELETE request.
  • It must be placed inside a <form> tag that uses method="POST".
blade
<form method="POST" action="/resource/1">
    @csrf
    @method('PUT')
    <!-- form inputs -->
    <button type="submit">Update</button>
</form>
💻

Example

This example shows a form that updates a resource using the PUT HTTP method. Since HTML forms only support GET and POST, @method('PUT') tells Laravel to treat the request as a PUT.

blade
<form method="POST" action="/posts/123">
    @csrf
    @method('PUT')
    <label for="title">Title:</label>
    <input type="text" id="title" name="title" value="Old Title">
    <button type="submit">Update Post</button>
</form>
Output
When submitted, Laravel treats this form as a PUT request to /posts/123, allowing the update logic in the controller to run.
⚠️

Common Pitfalls

  • Not including @csrf token with @method can cause CSRF errors.
  • Using method="PUT" directly in the form tag does not work because HTML only supports GET and POST.
  • Placing @method outside the form or forgetting it means Laravel will treat the request as POST.
blade
<!-- Wrong: method attribute set to PUT (won't work) -->
<form method="PUT" action="/resource/1">
    @csrf
    <!-- No @method directive -->
    <button type="submit">Update</button>
</form>

<!-- Right: method POST with @method('PUT') -->
<form method="POST" action="/resource/1">
    @csrf
    @method('PUT')
    <button type="submit">Update</button>
</form>
📊

Quick Reference

Use this cheat sheet to remember how to spoof HTTP methods in Blade forms:

HTTP VerbBlade Directive Usage
PUT@method('PUT')
PATCH@method('PATCH')
DELETE@method('DELETE')
POSTNo need to spoof, use
directly
GETUse directly, no spoofing needed

Key Takeaways

Use @method inside a POST form to spoof HTTP verbs like PUT, PATCH, or DELETE.
Always include @csrf token with forms that modify data to avoid security errors.
HTML forms only support GET and POST, so @method helps Laravel handle other verbs.
Do not set method attribute to PUT or DELETE directly in the form tag; always use POST with @method.
Place @method directive inside the form tag to ensure Laravel processes the correct HTTP method.