JamesSalzer's avatar

DB connection Check with Eloquent

Is there a way to check the connection to the DB with Eloquent, like with "use Illuminate\Support\Facades\DB;"? The statement I’m using at the moment is: "$isConnected = DB::connection('umsdb')->getPdo();"

0 likes
4 replies
Sinnbeck's avatar

Not sure what you are trying to do? Connect to the database to then see if it is connected when you connect?

JamesSalzer's avatar

I want to check if a DB connection was successful to write to a log file if not connected.

Sinnbeck's avatar

@JamesSalzer Ok. Then I think what you have is the cleanest. But you need to do a try catch, as it throws an exception if the connection fails.

try {
    DB::connection('umsdb')->getPdo();
    $isConnected = true;
} catch (\PDOException $e) {
    $isConnected = false;
}
    

Please or to participate in this conversation.