处理 DB 无法在 Laravel 中连接?

Ash*_*kru 5 php laravel

我想知道当 Laravel 无法连接到数据库时,是否有一种方法可以显示自定义视图?我试过在谷歌上搜索一个答案,但它确实没有显示任何有用的东西。

我目前得到:

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)
Run Code Online (Sandbox Code Playgroud)

谢谢。

sho*_*101 4

在你的app/Exceptions/Handler.php,转到render方法。您可以添加以下异常检查来处理querypdo异常

    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');
    }
Run Code Online (Sandbox Code Playgroud)

替换dd($e->getMessage());为您的自定义代码。