0
0
LaravelDebug / FixBeginner · 3 min read

How to Fix Token Mismatch Error in Laravel Quickly

The Laravel TokenMismatchException happens when the CSRF token in your form does not match the one stored in the session. To fix it, ensure you include @csrf in your Blade forms and that your session is properly configured and active.
🔍

Why This Happens

This error occurs because Laravel protects forms from attacks by checking a special token called CSRF token. If the token sent with the form does not match the one stored in the user's session, Laravel throws a TokenMismatchException. This usually happens when the token is missing, expired, or the session is not working correctly.

html
<form method="POST" action="/submit">
  <input type="text" name="name">
  <button type="submit">Send</button>
</form>
Output
Illuminate\Session\TokenMismatchException: CSRF token mismatch.
🔧

The Fix

To fix this, always include the CSRF token in your forms by adding @csrf inside your Blade form tags. This directive inserts a hidden input with the correct token. Also, make sure your session is enabled and working, as Laravel stores the token there.

blade
<form method="POST" action="/submit">
  @csrf
  <input type="text" name="name">
  <button type="submit">Send</button>
</form>
Output
Form submits successfully without token mismatch error.
🛡️

Prevention

Always use @csrf in your Blade forms to prevent missing tokens. Avoid caching pages with forms that include tokens, as cached tokens become invalid. Check that your session driver is properly configured and that cookies are enabled in the browser. Use Laravel's built-in form helpers to reduce mistakes.

⚠️

Related Errors

Other common errors include Session Expired when the session times out, and Invalid or Missing Cookies which prevent the CSRF token from being verified. Fix these by increasing session lifetime and ensuring cookies are enabled.

Key Takeaways

Always include @csrf in your Blade forms to provide the CSRF token.
Ensure your session is properly configured and active for token storage.
Avoid caching pages with forms that contain CSRF tokens.
Check browser cookies are enabled to maintain session and token validity.
Use Laravel's form helpers to reduce manual errors with tokens.