Handle TokenMismatchException

Written by Rich Banks on 19th May, 2016


I recently came across a few users leaving a form open for a long while and when processing it getting an ugly Token Mismatch Error.

Laravel Token Mismatch

Handling this error is very easy within Laravel but took me some time to figure out, so I thought I would write a very short article.

Open app/Exceptions/Handler.php in your preferred text editor. At the top of the file include:

use Illuminate\Session\TokenMismatchException;
use Redirect;

Then within the render method add:

if($e instanceof TokenMismatchException){
        return Redirect::back()->withInput()->withErrors(['Your session expired. Please try again!']);

Whenever a TokenMismatchException is thrown the user will be redirected back to the form with a short error message.