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

princeoo7's avatar

Custom error messages for only api for cases like Integrity constraint violations and all in laravel 6

Coming straight to the point, how to have custom errors for the errors such as integrity violations or any such error which needs to tell user in simple words, what when wrong ...

like instead of,

{
        "success": false,
        "error": "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry..."
}

who can i tell them

{
        "success": false,
        "error": "Your email id is already registered with us. please try to login or use forgot password to recover the account ..."
}

some thing of this sort, only on api level.

0 likes
1 reply
AO's avatar

You can use unique validation rule for the email field.

OR if you want to show custom view or JSON response for the query errors you can do something like this in App\Execptions\Handler.php :


    public function render($request, Exception $exception)
    {
        if ( $exception instanceof \Illuminate\Database\QueryException ) {
            // show custom view
            //Or
            //return response()->json($exception);
        }
        return parent::render($request, $exception);
    }

Please or to participate in this conversation.