Is Your Laravel Database Connected? Let's Find Out!

How to check database connection in Laravel

Is-Your-Laravel-Database-Connected-Let's-Find-Out

In this article we will learn how to check database connection in Laravel. Laravel provides lots of built-in functions to make our database queries easier , to check database connection with Laravel we will use one of the database query builder method. You can also use other methods to get database name and using of schema methods to check table and column exist or not .

HomeController.php :-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Artisan;
use DB;

class HomeController extends Controller
{
    public function checkDatabaseConnection(){
        try{
            $connection = DB::connection()->getPdo();
            if($connection){
                return 'connection successfull';
            }
            else{
                return 'unable to connect with database';
            }
        }
        catch(\Exception $e){
            return 'unable to connect with database';
        }
    }
}


Thank you for reading this article 😊

For any query do not hesitate to comment 💬

Previous Post Next Post

Contact Form