How to check if connected to database in Laravel?

0 votes
How can I check if laravel is connected to the database? I've searched around and I can't find anything that would tell me how this is done.
Sep 27 in Laravel by kartik
• 30,630 points
46 views

1 answer to this question.

0 votes

Hello @kartik,

You can use

if(DB::connection()->getDatabaseName())
{
   echo "Connected sucessfully to database ".DB::connection()->getDatabaseName().".";
}

It will give you the database name for the connected database, so you can use it to check if your app is connected to it.

But... Laravel will only connect to database once it needs something from database and, at the time of a connection try, if it finds any errors it will raise a PDOException, so this is what you can do to redirect your user to a friendly page:

App::error(function(PDOException $exception)
{
    Log::error("Error connecting to database: ".$exception->getMessage());

    return "Error connecting to database";
});

Add this to your app/filters.php file.

Hope it helps!!

Thank you!!

answered Sep 27 by Niroj
• 66,420 points

Related Questions In Laravel

0 votes
1 answer

How to check request is ajax or not in Laravel?

Hello, Laravel allow use of their library method that ...READ MORE

answered Mar 23 in Laravel by Niroj
• 66,420 points
243 views
0 votes
1 answer

How to populating a database in a Laravel migration file?

Hello @kartik, Don't put the DB::insert() inside of ...READ MORE

answered Aug 4 in Laravel by Niroj
• 66,420 points
73 views
0 votes
1 answer

How to return database table name in Laravel?

Hello @kartik, There is a public getTable() method ...READ MORE

answered Sep 24 in Laravel by Niroj
• 66,420 points
25 views
0 votes
1 answer

How to validate an input field if value is not null in Laravel?

Hello @kartik, try using nullable as a rule 'password' ...READ MORE

answered Sep 25 in Laravel by Niroj
• 66,420 points
93 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28 in JQuery by Niroj
• 66,420 points
3,060 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12 in JQuery by Niroj
• 66,420 points
226 views
0 votes
1 answer

How to enable Bootstrap tooltip on disabled button?

Hii @kartik, You can wrap the disabled button ...READ MORE

answered May 12 in JQuery by Niroj
• 66,420 points
803 views
+1 vote
2 answers

How to set cache false for getJSON in jQuery?

You can't pass any configuration parameters to ...READ MORE

answered Oct 7 in JQuery by Amit
• 140 points
227 views
+1 vote
1 answer

How to check if a record already exists in a laravel?

Hey, As a newbie it is most common ...READ MORE

answered Mar 31 in Laravel by Niroj
• 66,420 points
5,076 views
0 votes
1 answer

What is database migration and how to create database migration in Laravel?

Hii, Migrations are like version control for your database, ...READ MORE

answered Mar 23 in Laravel by Niroj
• 66,420 points
208 views