Laravel 5.1 - Checking a Database Connection

前端 未结 6 1522
粉色の甜心
粉色の甜心 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:19

    You can use this, in a controller method or in an inline function of a route:

       try {
            DB::connection()->getPdo();
            if(DB::connection()->getDatabaseName()){
                echo "Yes! Successfully connected to the DB: " . DB::connection()->getDatabaseName();
            }else{
                die("Could not find the database. Please check your configuration.");
            }
        } catch (\Exception $e) {
            die("Could not open connection to database server.  Please check your configuration.");
        }
    

提交回复
热议问题