Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check database connections in Laravel?
Laravel provides several ways to check database connections. The database configuration is stored in config/database.php, and connection details are defined in the .env file.
Database Configuration
The default database configuration in config/database.php ?
'default' => env('DB_CONNECTION', 'mysql'),
Your .env file should contain the database credentials ?
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=test DB_USERNAME=root DB_PASSWORD=
Using Controller Method
Test database connection within a controller using DB::connection() ?
<?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";
}
}
}
The output will be ?
Connected successfully to the database. Database name is :test
Using AppServiceProvider
Test connection during application bootstrap by adding code to the boot() method in app/Providers/AppServiceProvider.php ?
/**
* 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');
}
}
Note: This method will display the connection status on every page load, which may not be ideal for production.
Using Custom Artisan Command
Create a dedicated command for testing database connections ?
php artisan make:command testdbconnection
The command file will be created in app/Console/Commands/testdbconnection.php ?
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
class testdbconnection extends Command {
protected $signature = 'testdbconnection';
protected $description = 'Testing DB connection';
public function __construct() {
parent::__construct();
}
public function handle() {
try {
DB::connection()->getPDO();
dump('Database is connected. Database Name is : ' . DB::connection()->getDatabaseName());
} catch (Exception $e) {
dump('Database connection failed');
}
}
}
Register the command in app/Console/Kernel.php ?
protected $commands = [ Commands\testdbconnection::class ];
Execute the custom command ?
C:\xampp\htdocs\laraveltest>php artisan testdbconnection "Database is connected. Database Name is : test"
Conclusion
Laravel offers multiple methods to check database connections controller methods for web routes, service providers for applicationwide checks, and custom Artisan commands for manual testing. Choose the method that best fits your specific use case.
