Join my Laravel for REST API's course on Udemy 👀

Return Laravel validation errors as JSON

July 30, 2022  ‐ 1 min read

When you submit data to your Laravel API you probably don't want the validation errors returned to you as HTML.

If this happens to you, you can tell Laravel to return the validation errors as a JSON by setting the HTTP header 'Accept: application/json' in your request.

If you do need to return the validation messages somehow as JSON by yourself, you can add the following in one of your controller methods.

<?php

if ($validator->fails()) {
    return response()->json($validator->messages(), Response::HTTP_UNPROCESSABLE_ENTITY);
}