Search code examples
laraveldatabaseerror-handlinglaravel-bladelaravel-9

Laravel 9 throw custom error view if DB connection fails?


what i am trying to do is if the connection to the database fails, instead of getting No connection could be made because the target machine actively refused it, i want to throw a custom view that displays a simple h1 with the text that the connection fails. How can i do that?


Solution

  • The answer was given at the top, but i made it work with some modifications.

    use Throwable;
    use PDOException;
    
    public function render($request, Throwable $exception)
    {
        if ($exception instanceof PDOException) {
            return response()->view('errors.database', [], 500);
        }
    
        return parent::render($request, $exception);
    }