How to Get Database Name using Laravel?

Spread the love

Hello Guys,

In this tutorial, we’ll guide you through the process of obtaining get database name using Laravel. We’ll provide you with a practical example of how to retrieve the database name within the Laravel framework, helping you understand this essential concept.

This example is compatible with various Laravel versions, including Laravel 6, Laravel 7, Laravel 8, Laravel 9, and Laravel 10.

In the following demonstration, we’ll showcase how to access the database name in Laravel using the convenient DB class’s getDatabaseName() feature.

Let’s delve into the code example below:

Example of Get Database Name using Laravel:

Also Read: how to use Flash Message using LARAVEL 10

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:

Also Read: How To Import Excel File Into Database in Python

Your database is "your_database_name".

Uncover the ultimate insights in our collection, your gateway to the best Laravel blog content.

Leave a Comment