2021-06-28

Laravel 7 check database connection status and show custom error

I'm using Laravel-7 and MySQL SERVER for my project. How can I get the status of DB connection? I have already tried few methods, but it did not work. I want to get the status and display the custom success or error status.

My code is below:

        try{
            DB::connection()->getDatabaseName();
            return redirect('/registration')->with('db_con_status','Database connected!');

        }catch(Illuminate\Database\QueryException $dbexp){
            return redirect('/registration')->with('db_con_error',$dbexp->getMessage());
        }

but it still shown Laravel in-built ignition error like...

Illuminate\Database\QueryException

SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. (SQL: select count(*) as aggregate from users where email = abc@xyz.com)

How to catch that exception and handle it?



from Recent Questions - Stack Overflow https://ift.tt/3gVwZUW
https://ift.tt/eA8V8J

No comments:

Post a Comment