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.
Echo the Laravel database name in Blade/PHP This will output the name of the database or return 'none' if there is no connection. If you view it in the browser, it gives you the name of the connected database. Checking whether the application is connected to a Laravel database.
Laravel makes connecting with databases and running queries extremely simple. The database configuration file is app/config/database. php . In this file you may define all of your database connections, as well as specify which connection should be used by default.
It disconnects at the end of The Script. Laravel isn't using persistent connections and they can be bad at times anyway please dig into the PHP manual and there's a in-depth explaination of when and when not to use.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With