lundi 26 juin 2017

Laravel always return the error bag of an Validation error in json

I'm developing an api that should also provide messages about the validation problems.

When "hardcoding" validators I'm doing something like this

if ($validator->fails()) {
    return response()->json($validator->errors(), 400);
}

this works nice - however I want a "generic" solution to basically catch all ValidationExceptions and do the same as above.

I already tried to play around in the render function of Handler.php

public function render($request, Exception $exception)
{
    $message = $exception->getMessage();

    if (is_object($message)) {
        $message = $message->toArray();
    }

    if ($exception instanceof ValidationException) {
        return response()->json($message, 400);
    }

    ...
}

However I can't find a proper way of returning the actual relevant data that I want



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire