如何测试laravel中的连接是否正常?

时间:2016-05-13 10:48:38

标签: laravel laravel-4 laravel-5 laravel-5.1 laravel-5.2

我试过

if (DB::connection()) { 
    //
}

但这会产生错误:

[PDOException]
SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)

我想从代码中测试连接是否正常,但没有错误, 我正在寻找解决方案而不尝试捕捉.....

3 个答案:

答案 0 :(得分:1)

正如@Luke指出的那样,你最好的选择之一是使用try和catch。

try{
    Schema::hasTable('mytable');
    // or \DB::statement('show Databases');
}catch(\PDOException $e){
    //This means there is definitely error connecting to database
    //I don't understand why you don't want to catch me :(
}

您可以在帮助函数或特征中使用上述代码,或者您选择的任何理智的想法。 其他可能性使用shell_exec()system()

答案 1 :(得分:0)

您可以在try catch语句中使用此代码,以便您可以捕获异常,然后输出更好的消息,或重定向到其他位置。

供参考:

http://php.net/manual/en/language.exceptions.php http://www.w3schools.com/php/php_exception.asp

答案 2 :(得分:-3)

只做简单的工匠命令。

debug: websocket writing 1::
debug: setting request GET /socket.io/1/websocket/SFePun_Ug5ycS5EVIrSO
debug: set heartbeat interval for client SFePun_Ug5ycS5EVIrSO
debug: discarding transport
debug: cleared heartbeat interval for client SFePun_Ug5ycS5EVIrSO
debug: setting request GET /socket.io/1/websocket/SFePun_Ug5ycS5EVIrSO
debug: set heartbeat interval for client SFePun_Ug5ycS5EVIrSO
debug: discarding transport
debug: cleared heartbeat interval for client SFePun_Ug5ycS5EVIrSO
debug: setting request GET /socket.io/1/websocket/SFePun_Ug5ycS5EVIrSO
debug: set heartbeat interval for client SFePun_Ug5ycS5EVIrSO
debug: discarding transport
(repeated a couple of times)
...
IIS Detailed Error - 503.13 - Number of active WebSocket requests has reached the maximum concurrent WebSocket requests allowed.

socket.connect("http://myapp.azurewebsites.net/", this);