这个
"No query results for model [App\\Models\\Role]."
ModelNotFound
拉拉维尔例外。
更改这样的异常响应的最佳方法是使用异常处理程序的呈现函数来响应所需的任何消息。
例如,你可以
if ($e instanceof ModelNotFoundException) {
$response['type'] = "ups!;
$response['message'] = "Could not find what you're looking for";
$response['status'] = Response::HTTP_NOT_FOUND
}
return response()->json(['alert' => $response], $response['status']);
另一种方法是确保
找不到模型
->find()
而不是
->findOrFail()
如果没有返回结果,则使用类似的abort helper:
abort(400, 'Role not found');
或
return response(['alert' => [
'type' => 'ups!',
'title' => 'There is no role with the provided id!']
],400);