How to Fix 419 Page Expired Error in Laravel Quickly
@csrf in your Blade forms and that your session is properly configured and active.Why This Happens
The 419 Page Expired error occurs when Laravel's security system detects a missing or invalid CSRF token in a form submission. Laravel uses CSRF tokens to protect your app from malicious attacks by verifying that the form request is genuine. If the token is missing or expired, Laravel blocks the request and shows this error.
<form method="POST" action="/submit"> <input type="text" name="name"> <button type="submit">Send</button> </form>
The Fix
To fix the 419 error, add the @csrf directive inside your Blade form. This adds a hidden input with a valid CSRF token. Also, make sure your session is working correctly because Laravel stores the token in the session.
<form method="POST" action="/submit"> @csrf <input type="text" name="name"> <button type="submit">Send</button> </form>
Prevention
Always include @csrf in your forms to avoid missing tokens. Check that your session driver is properly set in config/session.php and that your app's encryption key is set in .env. Avoid long session lifetimes that can cause token expiration. Use Laravel's built-in form helpers to reduce mistakes.
Related Errors
Other errors related to CSRF tokens include:
- TokenMismatchException: Happens when the token does not match the session token.
- Session Expired: Occurs if the session times out before form submission.
Fixes usually involve checking session settings and ensuring tokens are included in forms.