How Can We Get Database Name in Laravel?

Share Me
  •  
  •  
  •  
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares

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.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →