Laravel form html with PUT method for PUT routes

Laravel form html with PUT method for PUT routes

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why HTML Forms Don't Submit via PUT Without a CSRF Token

You have defined your routes correctly, mapping PUT post/{post} to postcontroller@update. Your form action points to the correct URI (/post/{postID}), and the method attribute is set to put. However, the submission fails. This isn't an issue with CSS styling or URL construction; it is a fundamental security constraint in modern web frameworks: Cross-Site Request Forgery (CSRF) protection.

The Security Mechanism

By default, Laravel disables support for unsafe HTTP methods like PUT, PATCH, and DELETE on specific routes unless the request includes a valid CSRF token. This mechanism prevents malicious websites from tricking authenticated users into modifying data on your site without their consent.

When you use a standard <form> tag with method="put", the browser sends the request directly to the server. If your route has not been explicitly whitelisted or if no CSRF token is present in the payload, Laravel's middleware will reject the request before your controller method ever executes. This explains why "it doesn't work"—the server responds with a 419 (Page Expired) or 405 (Method Not Allowed), and you likely see no output because the redirect happens immediately.

The Traditional Solution: Form Helpers

The most common recommendation is to use Blade's form helpers, such as {{ Form::put(...) }}. While this generates the correct HTML structure automatically, it abstracts away the ability to apply custom CSS classes directly to the input elements or the form container itself in older versions of Laravel. If your primary requirement is maintaining full control over CSS styling—perhaps using Bootstrap or a custom stylesheet—the helper approach feels restrictive because the generated markup is static once rendered.

The Modern Approach: Manual Token Injection

In modern Laravel applications (specifically version 5.2 and above), you can safely use raw HTML forms with custom styling by manually injecting the CSRF token into your <form> tag. This gives you complete freedom over CSS classes while satisfying security requirements.

You need to ensure two things in your view:

  1. The method attribute is set to put.
  2. A hidden input field containing the session-based CSRF token exists within the form body.

Here is how to implement this with full CSS control:

<form class="col-md-12" action="{{ url('/post/' . $post->postID) }}" method="PUT">
    @csrf
    
    <div class="form-group">
        <!-- You can now apply any custom class here -->
        <textarea type="text" class="form-control input-lg custom-textarea" placeholder="Text Here" name="post">{{ old('post', $post->post) }}</textarea>
    </div>

    <div class="form-group">
        <!-- Custom button styling is fully supported -->
        <button class="btn btn-primary btn-lg btn-block edit-btn" type="submit">Edit</button>
    </div>
</form>

Implementation Details

Notice the @csrf directive added inside the form tags. This directive outputs a hidden input field like <input type="hidden" name="_token" value="...">. When Laravel's middleware intercepts the incoming request, it extracts this token and compares it against the one stored in your user's session. If they match, the postcontroller@update method executes successfully.

If you are using an older version of Laravel where @csrf might not be available or configured differently, you can manually insert the token using the old_input helper or directly accessing the session:

<input type="hidden" name="_token" value="{{ old_input('_token') }}">

Styling Considerations

By using the raw <form> tag with manual class application (as shown in the example above), you bypass the limitations of the Form helpers. You can now wrap the form in a custom container, apply specific padding to the .edit-btn class, or use grid systems that require specific class names on the parent div without worrying about how the helper generates them.

Just remember to include {{ old('post', $post->post) }} (as seen in the textarea example) to preserve the data if the validation fails and the user needs to correct their input. Without this, a failed validation would clear the form, forcing the user to retype their content.

Summary of Actions

To fix your issue while retaining CSS flexibility:

  1. Keep your existing <form> structure with action and method.
  2. Insert the @csrf directive immediately inside the opening <form> tag.
  3. Ensure your view file is being rendered by Blade (.blade.php).

Once the CSRF token is present in the request payload, Laravel will treat the PUT request as legitimate, allowing your post.update action to execute without error.

Our network