How to get database name in Laravel 9 ?
Today we are going to learn about "how to get the database name in Laravel 9
". Here we have different ways to get the name of the database which is connected to the Laravel application.
Simply you can visit the .env file to see the database name which is at the root of your project directory and you can also write some code in the controller or view file to get the database name in Laravel.
Here we can use below code in controller as well as view file to get the database name in Laravel.
env('DB_DATABASE');
-
Get the database name using env helper function
You can use the env helper function in controller or blade file to get the database name in Laravel.
Here you can place below code in your view file to get the connected database name.
{{ env('DB_DATABASE') }}
You can also use the env helper function in your controller’s method. You can use the echo function with it to display the database name on your computer screen.
//Controller's method $databaseName = env('DB_DATABASE');
-
Get the database name using config helper function in Laravel
You can use the config helper function to get the database name in Laravel. Here we have two different ways to get the database name using the config helper function.
You can change the connection name from mysql to sqlite or pgsql as per your requirement. You can use the config helper function in your controller’s method or view file.
Place code in Controller’s method (app\Http\Controllers<HomeController>.php)
$databaseName = config('database.connections.mysql.database'); $databaseName = Config::get('database.connections.'.Config::get('database.default'));
Place code in view file (resources\views<welcome>.blade.php)
{{ config('database.connections.mysql.database') }}
-
Get the database name using DB facade in Laravel
You can use the DB facade with connection() and getDatabaseName() method to get the database name in Laravel. First, you have to import the DB facade (use Illuminate\Support\Facades\DB;) at the top of the controller file to use it in your controller’s method.
use Illuminate\Support\Facades\DB;
Place code in controller’s method (app\Http\Controllers\HomeController.php)
$databaseName = \DB::connection()->getDatabaseName();
Place code in view file (resources\views\welcome.blade.php)
{{ $databaseName = \DB::connection()->getDatabaseName() }}
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to use more than one query scope in Laravel
- How to increment column value of table in Laravel
- 419 page expired error in Laravel
- How to insert multiple rows in mysql using loop in laravel?
- How to get only time from created_at in laravel
- How to pass query string with pagination in laravel
- Trying to access array offset on value of type null error in laravel
- How to get column names from table in Laravel
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- Send id with route Laravel
- Calculate age from date of birth in Laravel
- Target class [admin] does not exist.
- Convert multidimensional array to single array in Laravel
- How to get id of next record in laravel
- Validation errors for multiple forms on same page Laravel
- Create model with migration and seeder
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- Get the post details if it has at least one comment in comments table
- How to use bootstrap pagination in laravel 8
- Redirect to previous page or url in laravel
- Call to a member function update() on null
- How to add script on specific view file in laravel while extending layout
- Laravel get single row by id
- Conditional where clause in Laravel
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given