Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uxweb's avatar
Level 20

Better option to handle common L5 Exceptions

Hi, i've been using Laravel since v4 and now i'm completely using v5.

What i have always asked myself is:

How is supposed to be handled the following Laravel exceptions instead of letting them to bubble up to the global handler:

  • TokenMismatchException
  • ModelNotFoundException
  • NotFoundHttpException
  • MassAssignmentException
  • MethodNotAllowedHttpException

The point is that, when they happen, should i redirect the user to /home?, think that would be the easier, but for example with the TokenMismatchException i found that when some users let the /auth/login page for some time, then, they come back and do a login, the exception happens and they ask "what did i do wrong?", "why is this error is appearing?".

I know it is annoying for them, so, i still don't know how to handle them.

I've seen @JeffreyWay handles the NotFoundHttpException redirecting to the laracasts home page, and that makes sense for this kind of exception, but what about the others?, do you have learned the better way to handle them?, would you like to share how you do it?

Thank you!

0 likes
3 replies
bestmomo's avatar

For ModelNotFoundException I send a 404 error like that :

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    if($e instanceof ModelNotFoundException)
    {
        abort(404);
    }
    return parent::render($request, $e);
}
1 like
RachidLaasri's avatar
  • TokenMismatchException & MassAssignmentException : I redirect home.

  • NotFoundHttpException : 404 error like bestmomo said.

1 like
uxweb's avatar
Level 20

Thanks for the brainstorming!

Please or to participate in this conversation.