
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 insert value to additional columns in pivot table in laravel
- Route not defined in Laravel
- Use withCount() to get total number of records with relationship
- Delete all related comments on deleting a post in Laravel
- How to use or operator in laravel
- How to Run CRON Job on LIVE SERVER on Cpanel in Laravel Project
- Pass variable from blade to controller Laravel
- Convert input array to comma-separated string in laravel controller
- How to create event and listener in laravel ?
- How to pass link from controller to view in laravel on ajax call
- If condition in foreach loop in laravel
- Get products with number of orders in Laravel
- How to check records exist in loaded relationship in Laravel blade view
- The POST method is not supported for this route. Supported methods: PUT.
- Print last executed query in laravel
- How to create static page in Laravel
- Always load the relationship data with eager loading in Laravel
- How to Access Array in blade laravel
- Laravel onclick function not working
- How to get specific columns using with method in laravel Eloquent relationship
- How to display validation error in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Symlink(): No such file or directory
- How to get id of next record in laravel
- How to customize pagination view in laravel