在 Laravel 7 中,这段代码工作正常。usingrenderable方法也适用于 laravel 8。但我不确定如何在创建CustomException类后在 laravel 8 中注册它。
public function render($request, Exception $exception)
{
if ($exception instanceof ValidationException) {
if ($request->expectsJson()) {
return response('Sorry, validation failed.', 422);
}
}
return parent::render($request, $exception);
}
Run Code Online (Sandbox Code Playgroud)