I would just try to hit the database with a route in the browser.
Jan 28, 2016
12
Level 3
Easy way to check if laravel app has connection with existing MySQL DB
I'm going to use an existing MySQL database and have setup in config/database.php :
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'my-existing-db'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
Now I need to check if it's able to connect. What's the easiest way?
For example, in Rails it will throw error during server startup if you have any incorrect information on settings.
Level 12
A couple suggestions, from http://stackoverflow.com/questions/33432752/laravel-5-1-checking-a-database-connection
if(DB::connection()->getDatabaseName())
{
echo "conncted sucessfully to database ".DB::connection()->getDatabaseName();
}
or catch the error and redirect
App::error(function(PDOException $exception)
{
Log::error("Error connecting to database: ".$exception->getMessage());
return "Error connecting to database";
});
both sound like good options to me.
9 likes
Please or to participate in this conversation.