更新时间:2023-12-01 09:04:10
您正在寻找23000 Error code (Integrity Constraint Violation)
.如果您查看 QueryException
类,则它从 PDOException
,因此您可以访问$errorInfo
变量.
You are looking for the 23000 Error code (Integrity Constraint Violation)
. If you take a look at QueryException
class, it extends from PDOException
, so you can access to $errorInfo
variable.
要捕获此错误,您可以尝试:
To catch this error, you may try:
try {
// ...
} catch (\Illuminate\Database\QueryException $e) {
var_dump($e->errorInfo);
}
// Example output from MySQL
array (size=3)
0 => string '23000' (length=5)
1 => int 1452
2 => string 'Cannot add or update a child row: a foreign key constraint fails (...)'
更具体地说(重复的条目,不为null,添加/更新子行,删除父行...),这取决于每个DBMS:
To be more specific (Duplicate entry, not null, add/update child row, delete parent row...), it depends on each DBMS:
$e->getCode()
直接SQLSTATE
code, so you may return the first value from the array $e->errorInfo[0]
or call $e->getCode()
directly$e->errorInfo[1]
对于laravel,处理错误很容易,只需将此代码添加到"app/start/global.php"文件中(或创建
For laravel, handling errors is easy, just add this code in your "app/start/global.php" file ( or create a service provider):
App::error(function(\Illuminate\Database\QueryException $exception)
{
$error = $exception->errorInfo;
// add your business logic
});