Php 创建时的Laravel模型错误处理
我想使用Eloquent创建一个DB条目,如下所示:Php 创建时的Laravel模型错误处理,php,laravel,Php,Laravel,我想使用Eloquent创建一个DB条目,如下所示: MFUser::create(array( 'user_reference' => $this->userReference, 'first_name' => $this->firstName, 'last_name' => $this->lastName, 'user_type_id' => $this->userTypeI
MFUser::create(array(
'user_reference' => $this->userReference,
'first_name' => $this->firstName,
'last_name' => $this->lastName,
'user_type_id' => $this->userTypeId,
'email' => $this->email,
'password' => $this->password
));
try {
MFUser::create(array(
'user_reference' => $this->userReference,
'first_name' => $this->firstName,
'last_name' => $this->lastName,
'user_type_id' => $this->userTypeId,
'email' => $this->email,
'password' => $this->password
));
} catch (\Illuminate\Database\QueryException $exception) {
// You can check get the details of the error using `errorInfo`:
$errorInfo = $exception->errorInfo;
// Return the response to the client..
}
它工作得很好,除了将完全相同的数据放入字段的情况,这是意料之中的,因为不应该有重复。那我就去问了
但是,我如何正确地在这里执行错误处理,以检查是否发生了此查询异常,这样我就可以通过json将服务器的响应返回到客户端?只需将该代码包装在
try
-catch
块中即可。大概是这样的:
MFUser::create(array(
'user_reference' => $this->userReference,
'first_name' => $this->firstName,
'last_name' => $this->lastName,
'user_type_id' => $this->userTypeId,
'email' => $this->email,
'password' => $this->password
));
try {
MFUser::create(array(
'user_reference' => $this->userReference,
'first_name' => $this->firstName,
'last_name' => $this->lastName,
'user_type_id' => $this->userTypeId,
'email' => $this->email,
'password' => $this->password
));
} catch (\Illuminate\Database\QueryException $exception) {
// You can check get the details of the error using `errorInfo`:
$errorInfo = $exception->errorInfo;
// Return the response to the client..
}
只需使用
try/catch
块即可
use Illuminate\Database\QueryException;
// ...
try {
// DB query goes here.
} catch (QueryException $e) {
// Logics in case there are QueryException goes here
}
// ...
我宁愿为其他地方无法处理的意外事件保留
try/catch
。在这种情况下,您可以使用验证作为第一个度量,使用异常处理程序作为备份度量
如果表单请求失败,则会闪现错误消息,并将用户返回到上一页(提交表单的地方),您可以优雅地处理这些消息
在其他地方,在App\Exceptions目录中,您有一个异常处理程序类,它可以是各种错误的总括。当你无法优雅地处理它时,使用这个
// Second line of defence - something was missed, and a model was
// created without going via the above form request
namespace App\Exceptions;
class Handler extends ExceptionHandler
{
public function render($request, Exception $e)
{
if($e instanceof QueryException) {
// log it or similar. then dump them back on the dashboard or general something bad
// has happened screen
return redirect()->route('/dashboard');
}
}
}
Try-and-Catch
有些东西可能会帮助你想象为所有人创建Try-Catch,但有处理所有QueryException的最佳实践,我建议使用Laravel异常处理程序,因为它使你更容易使执行全局化
让我们试试看!,打开App\Exception\Handler.php
,在呈现方法中,你可以这样写
public function render($request, Throwable $exception)
{
if ($request->ajax() || $request->wantsJson()) {
if ($exception instanceof QueryException) {
// example algo for make response
return response()->json(['message' => 'xxx'], 403);
}
}
return parent::render($request, $exception);
}
在那之后,你可以为查询触发的每个错误请求获取xxx json,如果你使用验证,你仍然可以得到竞争条件。怎么会这样呢?我不认为会出现竞争情况?如果两个人同时注册同一封电子邮件,他们都会被验证,因为电子邮件尚未在数据库中,但当调用
create
方法时,其中一个人会失败,对吗?这是一个Huuuuuge edge案例。。。我期待着有一天我的应用程序如此流行,即多个用户使用相同的电子邮件地址注册:)不管怎样,假设您对数据库有限制,后一个将失败,触发上面第二个代码段中的异常。是的,对于使用相同电子邮件注册的用户,这可能是一个巨大的边缘案例。但在某些情况下,这可能很重要。为什么不使用下划线而不是大小写呢?这样你就可以通过请求直接传递信息,我错了吗?