Laravel 5.1 - Checking a Database Connection

前端 未结 6 1532
粉色の甜心
粉色の甜心 2020-12-13 02:10

I am trying to check if a database is connected in Laravel.

I\'ve looked around the documentation and can\'t find anything. The closest thing I\'ve found is this, bu

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 02:28

    Another Approach:

    When Laravel tries to connect to database, if the connection fails or if it finds any errors it will return a PDOException error. We can catch this error and redirect the action

    Add the following code in the app/filtes.php file.

    App::error(function(PDOException $exception)
    {
        Log::error("Error connecting to database: ".$exception->getMessage());
    
        return "Error connecting to database";
    });
    

    Hope this is helpful.

提交回复
热议问题