Laravel 4 - Cannot catch database exception in seed or migration class

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 into 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, etc., but I had no luck.
  • I can catch other types of exceptions (e.g. division by zero, etc.).
  • I can catch a QueryException inside routes.php, the same code works well: the code enters "catch", I get the message "SQL error: etc.", but the warning does not raise (correctly)

? .

+4
2

php , , .

-, XDebug, PHP Mac OS X ( PHP Windows, ), CLI (, php artisan db: seed --class= className).

XDebug:

xdebug.var_display_max_depth = -1
xdebug.var_display_max_children = -1
xdebug.var_display_max_data = -1

, , , QueryException , PHP .

(, 10), XDebug ( ), , / QueryExceptions.

+1

"Illuminate\Database\QueryException"

try {
    $data = array('id' => 1, 'name' => 'foo');
    DB::table('table')->insert($data);
}
catch (\Illuminate\Database\QueryException $e) {
    $this->command->error("SQL Error: " . $e->getMessage() . "\n");
}
+7

Source: https://habr.com/ru/post/1532962/


All Articles