Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 - Checking a Database Connection

Tags:

php

mysql

laravel

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, but this doesn't solve my problem.

I have three instances of MySQL that are set up on different machines. Below is a simplified version of what I am trying to achieve.

  1. If database 1 is connected, save data to it
  2. If database 1 is not connected, check if database 2 is connected
  3. If database 2 is connected save data to it
  4. If database 2 is not connected, check if database 3 is connected
  5. If database 3 is connected, save data to it

To be clear, is there a way to check that a database is connected in Laravel 5.1?

like image 552
Enijar Avatar asked Oct 30 '15 09:10

Enijar


People also ask

How can I get db name in Laravel?

we will give you the database name for the connected database, so you can use DB Facade in laravel. if you do not know your database name then you can use bellow example. Bellow example in Config Facade used to get connection database name. Config in get() method used to get your connected database in laravel.

Can Laravel connect to SQL Server?

Laravel makes connecting with SQL Server database and running queries extremely simple out of the box. The SQL Server, MSSQL database configuration for your application is located at Laravel Project Root Folder, config/database.

How to check if Laravel app is connected to a database?

Using the php artisan dump-server command to check the database connection. As you see, it's very easy to confirm that your Laravel app is connected to a database. I especially use this for my local apps.

What is database_url in Laravel?

For convenience, Laravel supports these URLs as an alternative to configuring your database with multiple configuration options. If the url (or corresponding DATABASE_URL environment variable) configuration option is present, it will be used to extract the database connection and credential information.

What is the use of Laravel framework?

Laravel is a great framework for rapid application building. It allows you to easily connect to a database. If you’re developing locally, chances are that you need to confirm that the application is connected to a database, for example when you’re debugging something.

What is the default sample environment configuration for Laravel sail?

By default, Laravel's sample environment configuration is ready to use with Laravel Sail, which is a Docker configuration for developing Laravel applications on your local machine. However, you are free to modify your database configuration as needed for your local database.


5 Answers

Try just getting the underlying PDO instance. If that fails, then Laravel was unable to connect to the database!

use Illuminate\Support\Facades\DB;

// Test database connection
try {
    DB::connection()->getPdo();
} catch (\Exception $e) {
    die("Could not connect to the database.  Please check your configuration. error:" . $e );
}
like image 79
alexw Avatar answered Oct 05 '22 19:10

alexw


You can use alexw's solution with the Artisan. Run following commands in the command line.

php artisan tinker
DB::connection()->getPdo();

If connection is OK, you should see

CONNECTION_STATUS: "Connection OK; waiting to send.",

near the end of the response.

like image 28
Luboš Miřatský Avatar answered Oct 05 '22 20:10

Luboš Miřatský


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

   use Illuminate\Support\Facades\DB;
   //....
   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.");
    }
like image 27
Luca C. Avatar answered Oct 05 '22 18:10

Luca C.


You can also run this:

php artisan migrate:status

It makes a db connection connection to get migrations from migrations table. It'll throw an exception if the connection fails.

like image 45
Sumit Wadhwa Avatar answered Oct 05 '22 19:10

Sumit Wadhwa


You can use this query for checking database connection in laravel:

use Illuminate\Support\Facades\DB;
// ...
$pdo = DB::connection()->getPdo();

if($pdo)
   {
     echo "Connected successfully to database ".DB::connection()->getDatabaseName();
   } else {
     echo "You are not connected to database";
   }

For more information, you can check out this page https://laravel.com/docs/5.0/database.

like image 31
Dawlatzai Ghousi Avatar answered Oct 05 '22 20:10

Dawlatzai Ghousi