Laravel 5.5.* has made a change which is big deal with Laravel developers now a days and the change is you can't customize your validation error response like you you did in earlier versions. In earlier versions of Laravel we were able to customize or change our validation error response with below function example:
1
2
3
4
5
6
7
8
9
10
11
12
|
public function response(array $errors) {
$first_error = '';
foreach ($errors as $error) {
$first_error = $error[0];
break;
}
$this->response['result'] = array();
$this->response['message'] = $first_error;
$this->response['status'] = 0;
return \Illuminate\Support\Facades\Response::json($this->response, Response::HTTP_BAD_REQUEST)->header('Content-Type', "application/json");
}
|
Now in current versions of Laravel 5.5.* you will not be able to customize your validation error response with this function.
You will require to use followings as well:
1
2
|
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
|
If you are using FormRequests to validate data and want to return custom JSON on error response in Laravel 5.5.*, You have to you below function.
1
2
3
|
protected function failedValidation(Validator $validator) {
throw new HttpResponseException(response()->json($validator->errors(), 422));
}
|
It will return all the validation error in JSON. ...