Laravel Customize Failed Validation Request
In this post, I will show you how to customize the form request validates failed response correctly, it’s really simple but useful. <?php namespace App\Http\Requests\Concerns; use App\Enums\Response; use Illuminate\Contracts\Validation\Validator; use Illuminate\Http\Exceptions\HttpResponseException; use Illuminate\Http\JsonResponse; use Illuminate\Validation\ValidationException; trait HandleFailedValidation { protected function failedValidation(Validator $validator): HttpResponseException { if ($this->expectsJson()) { // if you're json $errors = (new ValidationException($validator))->errors(); // take all errors as array. throw new HttpResponseException( new JsonResponse([ 'status' => Response::FAILED_VALIDATION, // customize status code....