Skip to content

Instantly share code, notes, and snippets.

@xxRockOnxx
Last active January 20, 2021 21:35
Show Gist options
  • Save xxRockOnxx/329c35b93ea9821a1b255ab36c1fee98 to your computer and use it in GitHub Desktop.
Save xxRockOnxx/329c35b93ea9821a1b255ab36c1fee98 to your computer and use it in GitHub Desktop.
[Laravel] Convert dot array validation error to nested array
<?php
namespace App\Exceptions;
class Handler
{
/**
* Convert a validation exception into a JSON response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Validation\ValidationException $exception
* @return \Illuminate\Http\JsonResponse
*/
protected function invalidJson($request, ValidationException $exception)
{
$errors = [];
foreach ($exception->errors() as $key => $value) {
array_set($errors, $key, $value);
}
return response()->json($errors, $exception->status);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment