5

I was wondering if there was a way where I can display a custom view when laravel fails to connect to the database? I have tried googling an answer for this but it really doesn't display anything useful.

I currently get:

Whoops, looks like something went wrong.

2/2
QueryException in Connection.php line 770:
SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) (SQL: select * from `users` where `users`.`id` = 1 limit 1)

Thanks.

4
  • Check your database details in .env file.Correct your database connection in .env file.It should work for you. Commented Jan 2, 2017 at 9:28
  • I don't want to fix it, I purposely broke it to try and display a custom view. My question is, HOW do I display a CUSTOM view. Commented Jan 2, 2017 at 9:28
  • what do you mean by custom view? Commented Jan 2, 2017 at 9:30
  • can you show your controller code and view path..? Commented Jan 2, 2017 at 9:31

1 Answer 1

4

In your app/Exceptions/Handler.php, go to render method . You can add the following exception checking to handle query and pdo exception

    if ($e instanceof \Illuminate\Database\QueryException) {
        dd($e->getMessage());
        //return response()->view('custom_view');
    } elseif ($e instanceof \PDOException) {
        dd($e->getMessage());
        //return response()->view('custom_view');
    }

Replace dd($e->getMessage()); with your custom code.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, is there any way I can do something similar to handle 500 error codes?
if you want to show a custom view for http error, I think you can put your error files in views/errors/error_code.blade.php ( in your case views/errors/500.blade.php). Laravel should pull that file automatically. Please accept the answer if it solved your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.