How to Get Database Name in Laravel?
Hey Guys,
In this article, we will cover how to get a database name in Laravel. we will help you to give an example of Laravel get database name. you can understand the 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 the database name in Laravel using getDatabaseName() function of the DB class. so, let's see the below example.
coding part:
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".