How to Get Database Name in Laravel 2022?

Hello dev, Today we are going to learn How to Get Database Name in Laravel? This tutorial will cover on how you can get database name in laravel application.

In this article, we will cover how to get database name in laravel. we will help you to give an example of laravel get database name. you can understand a concept of get database name in laravel. you will learn how to get eloquent database name in laravel.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.

In this example, i will show you how to get database name in laravel. we will use getDatabaseName() function of DB class. so, let’s see the below example.

So, let’s see the below code:

Also Read: How to Check Database Connection in Laravel 2022?

Example of How to Get Database Name in Laravel

app/Http/Controllers/DemoController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use DB;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
          
        $database = DB::connection()->getDatabaseName();
          
        dd("Your database is ".$database.".");
  
    }
}

Output:

database is "my_database".

Related Question also Ask

  • How can I get db name in Laravel?
  • What database does Laravel use?
  • How does Laravel connect to database?
  • What is DB facade in Laravel?

Conclusion

Today, We had learn How to Get Database Name in Laravel?. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: How to Get All Files in a Directory in Laravel (2022)

2 Comments

Leave a Reply