Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch SQL Exception in Laravel 5

Hay I'm creating a code like this :

\Log::info("saving log....");
    try{
        $data = AstronautCandidateLog::insert($request->logs);
    }catch (SQLException $e)
    {
        \Log::info("SQL Exception happened");
    }catch (Exception $e)
    {
        \Log::info("Exception happened");
    }

    \Log::info("status save data : ". $data);

But it seems that my Exception never get hit. So how to capture exception in laravel when something wrong in sql query...??

Thanks in advance.

like image 276
Maryadi Poipo Avatar asked Oct 14 '16 10:10

Maryadi Poipo


1 Answers

My case: add \ before Exception class otherwise it not handle

try
{
//write your codes here
}

catch(\Exception $e) {

            Log::error($e->getMessage());
        }
like image 78
Sunil Shakya Avatar answered Oct 08 '22 04:10

Sunil Shakya