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
Any idea? Thanks for your help.
You must catch "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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With