How to Get Database Name in Laravel?

Admin   Laravel   149  2021-03-17 04:50:05

Hi Guys,

In this blog, I will show you get database name in laravel 6. 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.

Example 1

 

public function getDatabaseName()

{

$databaseName = \DB::connection()->getDatabaseName();

dd($databaseName);

}

Output

 

blog

Bellow example in Config Facade used to get connection database name.

Config in get() method used to get your connected database in laravel.

Example 2

 

public function getDatabaseName()

{

$databaseName = Config::get('database.connections.'.Config::get('database.default'));

dd($databaseName['database']);

}

Output

 

blog

Example 3

 

public function getDatabaseName()

{

$databaseName = Config::get('database.connections');

dd($databaseName['mysql']['database']);

}

Output

 

blog

It will help you...