One of the core strengths of Laravel is its expressive syntax. Whether it’s routing, middleware, or Blade templates, Laravel emphasizes readability and developer experience. Among the many conveniences Laravel offers, one that stands out in day-to-day form handling is how it manages CSRF protection and form method spoofing. In recent Laravel versions (including Laravel 12), there’s a cleaner, shorter syntax available for both.
In this comprehensive guide, we’ll explore:
- What CSRF tokens are and why they matter
- What form method spoofing is
- The old vs. new syntax for CSRF and method spoofing
- Practical examples
- Best practices for writing cleaner, more secure forms
🛡 What Is CSRF in Laravel?
CSRF (Cross-Site Request Forgery) is a security threat where malicious sites trick users into submitting unauthorized requests to your application.
To prevent this, Laravel requires that every form submission includes a CSRF token — a unique string that validates the request’s origin.
Laravel includes this protection out of the box via middleware, and any form submission without the correct token will be rejected with a 419 Page Expired error.
✍️ Traditional Way to Include CSRF in Blade
Previously, you’d include a CSRF token like this in Blade:
<form action="/submit" method="POST">
@csrf
<input type="text" name="name">
<button type="submit">Submit</button>
</form>
The @csrf directive injects a hidden field like:
<input type="hidden" name="_token" value="a1b2c3d4...">
While this is already fairly clean, Laravel now offers an even shorter syntax.
⚙️ Introducing Shorter Syntax for CSRF and Method Spoofing
Laravel 11+ introduced compact Blade form components for CSRF and method spoofing using the @csrf and @method directives inline inside the <form> tag.
✅ New Short Syntax:
<form action="/submit" method="POST" @csrf>
<input type="text" name="name">
<button type="submit">Submit</button>
</form>
✅ For Method Spoofing:
<form action="/update-user" method="POST" @method('PUT')>
@csrf
<input type="text" name="name">
<button type="submit">Update</button>
</form>
This adds:
<input type="hidden" name="_method" value="PUT">
Automatically, Laravel will treat this POST request as a PUT request, allowing RESTful controllers to work properly.
🔄 Comparison: Old vs New Syntax
| Feature | Traditional Syntax | New Short Syntax |
|---|---|---|
| CSRF | @csrf inside form | @csrf inside <form> tag |
| Method Spoofing | @method('PUT') inside form | @method('PUT') in form tag |
| Readability | Good | Even better |
| Verbosity | Slightly more | Minimal and clean |
🧪 Full Form Example (Old vs New)
🔴 Old Way
<form action="{{ route('users.store') }}" method="POST">
@csrf
<input type="text" name="name">
<input type="email" name="email">
<button type="submit">Save</button>
</form>
🟢 New Shorter Way
<form action="{{ route('users.store') }}" method="POST" @csrf>
<input type="text" name="name">
<input type="email" name="email">
<button type="submit">Save</button>
</form>
🔧 Form Method Spoofing in Laravel
HTML forms support only GET and POST. To use PUT, PATCH, or DELETE, Laravel uses method spoofing — a hidden _method input field.
Example:
<form action="/posts/1" method="POST" @method('DELETE') @csrf>
<button type="submit">Delete Post</button>
</form>
Laravel will interpret this as a DELETE request to /posts/1.
🧼 Why Short Syntax Matters
- ✅ Cleaner templates — fewer lines of boilerplate
- 🧠 Better readability — instantly shows intent
- ⚡ Faster development — no need to remember to add
@csrfand@methodinside the form - 🛡 Security by default — you’re less likely to forget to include CSRF
💡 Laravel Best Practices with CSRF and Method Spoofing
Here are some real-world best practices:
1. Always Use @csrf on POST, PUT, PATCH, DELETE Forms
Laravel will reject unsafe HTTP methods without a CSRF token.
<form method="POST" action="/submit" @csrf>
2. Use RESTful Method Spoofing Consistently
Follow REST principles in your routes and forms:
Route::put('/users/{id}', 'UserController@update');
Route::delete('/users/{id}', 'UserController@destroy');
Then in your Blade:
<form method="POST" action="/users/{{ $user->id }}" @method('PUT') @csrf>
3. Keep Your Forms Declarative
Use consistent syntax so all your team members can recognize the pattern quickly:
<form method="POST" action="/something" @method('PATCH') @csrf>
🔍 Bonus: CSRF in AJAX Requests
For JavaScript or Axios requests, Laravel automatically sets the CSRF token via a meta tag:
<meta name="csrf-token" content="{{ csrf_token() }}">
In JavaScript:
axios.defaults.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').content;
If you’re using fetch, you can set it manually:
fetch('/submit', {
method: 'POST',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
'Content-Type': 'application/json',
},
body: JSON.stringify({name: 'John'}),
});
🧱 How It Works Under the Hood
Laravel’s VerifyCsrfToken middleware checks all incoming requests (except GET, HEAD, OPTIONS) for a valid _token value. If it’s missing or invalid, Laravel throws a TokenMismatchException.
When you use the short syntax @csrf, Laravel simply renders:
<input type="hidden" name="_token" value="{{ csrf_token() }}">
This token matches the session token stored server-side, ensuring that the request is legitimate.
⚠️ Common Mistakes to Avoid
| Mistake | Why It’s a Problem |
|---|---|
Forgetting @csrf | Laravel will throw a 419 Page Expired error |
Using GET for data modifications | Not RESTful, and not secure |
| Forgetting to spoof method | Laravel will treat it as a POST, and route may not match |
🌐 Real Use Cases
✅ Create a New Post
<form method="POST" action="/posts" @csrf>
<input name="title">
<textarea name="body"></textarea>
<button type="submit">Create</button>
</form>
✅ Update Post
<form method="POST" action="/posts/1" @method('PUT') @csrf>
<input name="title" value="Old Title">
<button type="submit">Update</button>
</form>
✅ Delete Post
<form method="POST" action="/posts/1" @method('DELETE') @csrf>
<button type="submit">Delete</button>
</form>
📘 Laravel Version Compatibility
| Laravel Version | Short Syntax Support |
|---|---|
| Laravel 9 | ❌ Not supported |
| Laravel 10 | ✅ Partial |
| Laravel 11+ | ✅ Fully supported |
| Laravel 12 | ✅ Recommended |
🧾 Conclusion
Laravel’s newer short CSRF and method syntax makes form development cleaner, faster, and less error-prone. It improves Blade readability while maintaining the robust security features Laravel is known for.
By using:
<form method="POST" action="/..." @csrf @method('PUT')>
You reduce clutter, minimize mistakes, and keep your code modern.
- Use
@csrfand@method()inside the<form>tag. - Laravel will auto-generate the correct hidden fields.
- Works great in Laravel 11 and 12.
- Always protect unsafe HTTP methods with CSRF.
- Cleaner Blade = Happier Devs!
Need help upgrading older Blade forms or automating method spoofing? Let me know — I’d be happy to help with a Blade component or macro!

Leave a Reply