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

shashi_verma's avatar

Laravel - How to show JSON when API route is wrong or not found ?

I am using Laravel to create API and postman to check my API. When I am putting a right URL in postman it is working fine, but when I am putting a wrong URL it is showing HTML and Laravel 404 error, I want to return a message in JSON format, how would I do that.

0 likes
3 replies
vajid's avatar

file exceptions/handler.php in handler method

if ($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
{
        return response()->json([
    'message'=>'not found'
    ]);
}
    return parent::render($request, $e);

Please or to participate in this conversation.