ItSolutionStuff.com

How to Get Database Name in Laravel?

By Hardik Savani • April 16, 2024
Laravel

Hey Guys,

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, laravel 9, laravel 10 and laravel 11 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:

Example:

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".

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel - How to Get All Files in a Directory?

Read Now →

How to Get All Routes in Laravel?

Read Now →

Laravel Download File from URL to Storage Example

Read Now →

How to Write Text on Existing PDF File in Laravel?

Read Now →

How to Get Browser Name and Version in Laravel?

Read Now →

How to Convert JSON to Array in Laravel?

Read Now →

Laravel Cashier Stripe Subscription Example Tutorial

Read Now →

How to Use Google Translator in Laravel?

Read Now →

How to Automatically Generate Sitemap in Laravel?

Read Now →

Laravel Contact Form Send Email Tutorial

Read Now →

How to Add Google Map in Laravel?

Read Now →

Laravel JQuery UI Autocomplete Ajax Search Example

Read Now →