How to check database connections in Laravel?


Laravel database configuration is stored inside config/database.php. The list of database configurations is listed inside this file. By default, you need to tell Laravel which database you are going to make use of.

The default database used is mysql and we are going to stick to it and check the database connection to mysql.

/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mysql'),

The .env file has the environment configuration for your database as shown below −

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=

When you start working with a database make sure to enter all the valid details in the .env file for database connection to mysql. Let us try a simple example that shows the database connection.

Example 1

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class StudentController extends Controller{ public function index() { try { $dbconnect = DB::connection()->getPDO(); $dbname = DB::connection()->getDatabaseName(); echo "Connected successfully to the database. Database name is :".$dbname; } catch(Exception $e) { echo "Error in connecting to the database"; } } }

Output

The output of the above code is −

Connected successfully to the database. Database name is :test

Example 2

Another is you can test the DB connection when you start Laravel. You can add the db connection code inside the boot() method in app/Providers/AppServiceProvider as shown below −

/** * Bootstrap any application services. * * @return void */ public function boot(){ try { DB::connection()->getPDO(); dump('Database is connected. Database Name is : ' . DB::connection()->getDatabaseName()); } catch (Exception $e) { dump('Database connection failed'); } }

Now when you do php artisan serve you should see the message for either database connected or database connection failed as shown in the screenshot below −

But the drawback of the above method is that you will see it inside your views too. For example −

Example 3

Another workaround is we can create a custom command and add the database checking code in it and use it whenever required. To create custom command use below command.

php artisan make:command testdbconnection

Output

The output of the above code is −

C:\xampp\htdocs\laraveltest>php artisan make:command testdbconnection
Console command created successfully.

C:\xampp\htdocs\laraveltest>

The files for testdbconnection will be inside app/console/commands.

testdbconnection.php

<?php namespace App\Console\Commands; use Illuminate\Console\Command; use DB; class testdbconnection extends Command{ /** * The name and signature of the console command. * @var string */ protected $signature = 'testdbconnection'; /** * The console command description. * @var string */ protected $description = 'Testing DB connection'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return int */ public function handle() { try { DB::connection()->getPDO(); dump('Database is connected. Database Name is : ' . DB::connection()->getDatabaseName()); } catch (Exception $e) { dump('Database connection failed'); } } }

Change the command name, description and add the database connection code inside handle() method as shown above. Now open kernel.php file and add below line in it −

protected $commands = [ Commands\testdbconnection::class ];

Now you can test you command as shown below −

C:\xampp\htdocs\laraveltest>php artisan testdbconnection
"Database is connected. Database Name is : test"

C:\xampp\htdocs\laraveltest>

Updated on: 30-Aug-2022

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements