- Difference between
redirect()->back()andredirect()->route('route')
redirect()->back()
Redirects to the previous URL (taken from the HTTP Referer header).
Used for when you want to send the user back to wherever they came from (like a “Cancel” button).
redirect()->route('route')
Redirects to a specific named route.
Used for when you know exactly which page to go to (e.g., back to the form page).
Why you’re getting 419 Page Expired with redirect()->back()
This happens because:
redirect()->back() returns the user to the previous request URL, which might be the same POST request (the one that submitted the form).
That means the browser might try to re-POST the form data (without a valid CSRF token anymore).
Hence, Laravel throws a 419 CSRF Token Mismatch error.
Essentially:
You’re redirecting back to the same POST route instead of the GET route of the form.
Correct Approach
return redirect()->route('your.form.route.name');
or better still
// routes/web.php
Route::get('/contact', [ContactController::class, 'showForm'])->name('contact.form');
Route::post('/contact', [ContactController::class, 'submitForm'])->name('contact.submit');
// ContactController.php
public function submitForm(Request $request)
{
$request->validate([
'email' => 'required|email',
'message' => 'required',
]);
// Handle the form...
return redirect()
->route('contact.form')
->with('success', 'Thank you for your message!');
}