How can I check if laravel is connected to the database? I've searched around and I can't find anything that would tell me how this is done.
How can I check if laravel is connected to the database? I've searched around and I can't find anything that would tell me how this is done.
You can use
if(DB::connection()->getDatabaseName())
{
echo "Connected sucessfully to database ".DB::connection()->getDatabaseName().".";
}
It will give you the database name for the connected database, so you can use it to check if your app is connected to it.
但是Laravel只会在需要数据库中的某个内容时连接到数据库,在try 连接时,如果发现任何错误,它将提高PDOException
分,因此您可以这样做,将用户重定向到友好页面:
App::error(function(PDOException $exception)
{
Log::error("Error connecting to database: ".$exception->getMessage());
return "Error connecting to database";
});
Add this to your app/filters.php
file.
在我看来,您不需要判断它是否连接,只需在异常处理闭包中采取适当的操作即可.