Home Laravel How Can We Get Database Name in Laravel?

How Can We Get Database Name in Laravel?

by Shahriar Sagor

Hello Dev’s
Now today in this blog, I will give a example of how to get database name in our laravel application. Now here i will give you a example of the database name for the connected database, so here you can use DB Facade in laravel application. Here we will see How Can We Get Database Name in Laravel.

If you do not know what is 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

Read Also :How to get all routes in Laravel Application?

Thanks for read. I hope it help you. For more you can follow us on facebook.

close

You may also like