sineld Laravel 11 için boostrap/app.php içinde
->withExceptions(function (Exceptions $exceptions) {
$exceptions->render(function (ValidationException $e, Request $request) {
if ($request->expectsJson()) {
$names = array_map(function ($items) {
return array_map(function ($key) {
if (str_contains($key, '\\')) {
$parts = explode('\\', $key);
return end($parts);
} else {
return $key;
}
}, array_keys($items));
}, $e->validator->failed());
return response()->json([
'message' => $e->getMessage(),
'errors' => $e->errors(),
'rules' => $names,
], $e->status);
}
return null;
});
});
Laravel 8/9/10 için app/Exceptions/Handler.php içinde
protected function invalidJson($request, ValidationException $e)
{
$names = array_map(function ($items) {
return array_map(function ($key) {
if (str_contains($key, '\\')) {
$parts = explode('\\', $key);
return end($parts);
} else {
return $key;
}
}, array_keys($items));
}, $e->validator->failed());
return response()->json([
'message' => $e->getMessage(),
'errors' => $e->errors(),
'rules' => $names,
], $e->status);
}