How to Get Database Name in Laravel?

Laravel 6 , Laravel

Nicesnippets

42668

06-12-2019


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...


Recommended Posts