Close
Stay up to date with all things Laravel, Tailwind, WordPress & PHP

Subscribe now to my e-mail newsletter and get my latest articles and project updates delivered directly to your inbox. Never miss an update.

How to check your Laravel database connection

Published April 16, 2021; last updated on April 22, 2021
How to check your Laravel database connection

Laravel is a great framework for rapid application building. It allows you to easily connect to a database. If you’re developing locally, chances are that you need to confirm that the application is connected to a database, for example when you’re debugging something.

In this tutorial I’ll show you how to check which database your Laravel app is connected to.

There is a simple code snippet to check the name of the current database connection, and if not, it will return ‘none’. There are two ways to it:

  1. Place it somewhere in a Blade template or PHP-file (recommended for one-time debugging)
  2. Place it in a random file and dump() it to the dump-server (recommended for all other use cases)

Echo the Laravel database name in Blade/PHP

The simplest way it to place the following script in a Blade or PHP file. This will output the name of the database or return ‘none’ if there is no connection.

<strong>Database Connected: </strong>
<?php
    try {
        \DB::connection()->getPDO();
        echo \DB::connection()->getDatabaseName();
        } catch (\Exception $e) {
        echo 'None';
    }
?>

If you view it in the browser, it gives you the name of the connected database.

Debugging in Laravel and confirm that Laravel is connected to a database.
Checking whether the application is connected to a Laravel database.

Using the dump-server to check this

But it is not always handy to show this immediately on the front-end. You can remedy that by using the php artisan dump-server. This allows you to log the database connection to your Terminal / Command Line Tool, whilst not displaying it in the browser.

First, start the dump server. Do this by opening your CLI, look Terminal, iTerm2 or an other. Personally I prefer iTerm2, but the default terminal is also a good choice if you don’t need a CLI often. Run this code to start the dump server:

php artisan dump-server

Next, place the following code somewhere in your files. You could still place it in Blade or PHP file, but also in a controller or the boot() function of the /app/Providers/AppServiceProvider.php file. Personally I recommend placing it in the boot() method.

try {
    \DB::connection()->getPDO();
    dump('Database connected: ' . \DB::connection()->getDatabaseName());
}

catch (\Exception $e) {
    dump('Database connected: ' . 'None');
}

Note: if you place the above code in a PHP or Blade files, make sure to wrap it in PHP tags: <?php and ?>.

If you then load a page, you see that your CLI automatically shows you which database it is connected to. 🚀

Using the Laravel Dump-server with PHP artisan to check database connection
Using the php artisan dump-server command to check the database connection.

Conclusion

As you see, it’s very easy to confirm that your Laravel app is connected to a database. I especially use this for my local apps. Let me know in the comments how it goes 👇

Check out my other articles and learn how to set set up local MySQL databases for Laravel and see how to set up Tailwind CSS & SASS with Laravel Mix.

Stay up to date with all things Laravel, Tailwind, WordPress & PHP

Subscribe now to my e-mail newsletter and get my latest articles and project updates delivered directly to your inbox. Never miss an update.

Image Ralph J. Smit
Ralph is a designer gone developer. He happily lives in the Netherlands. His passion for good design drove him towards development, because he felt that no-code tools were too limiting. On this blog, Ralph writes the articles he would've wanted to have during his continual developer journey. → Follow on Twitter

Comments

Leave a reply

Your email address will not be published. Required fields are marked *