Laravel 4 with MySql db. For some reason, I cannot catch DB exceptions (Illuminate\Database\QueryException) inside a seed or migration class: the code never enters the catch block.
For example, if I try to insert on a table where the column 'name' is UNIQUE:
try {
$data = array('id' => 1, 'name' => 'foo');
DB::table('table')->insert($data);
}
catch (\Exception $e) {
$this->command->error("SQL Error: " . $e->getMessage() . "\n");
}
...I always get this error:
PHP Warning: Uncaught exception 'Illuminate\Database\QueryException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
- I tried to catch Exception, \Exception, \PDOException, \Illuminate\Database\QueryException and so on, but I had no luck.
- I can catch other types of exceptions (e.g. Division by zero, etc.)
- I can catch QueryException inside routes.php, the same code works well: code enters the 'catch', I get the message "SQL Error: etc." but no warning is raised (correct)
Any idea? Thanks for your help.