Connect Your Laravel Database: A Quick Guide

 How to get connected database name in Laravel


In this article we will learn how to get connected database name 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 .

There are basically two cases like if your application is connected to a single DB , then you can use the env() method to get the connected database name as well as you can also use method we have described below , but if your application is connected with different databases through database.php file
, then the following method will be helpful to you for retrieving connected database name .

HomeController.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 getDatabaseName()
            $db_name = DB::connection()->getDatabaseName();
            return $db_name;
        catch(\Exception $e){
            return false;

Previous Post Next Post

Contact Form