mercredi 21 décembre 2016

For Laravel, How to return a response to client in a inner function

I'm building an api using laravel, the issue is when the client requests my api by calling create() function, and the create()function will call a getValidatedData() function which I want to return validation errors to the client if validation fails or return the validated data to insert database if validation passes, my getValidatedData function is like below so far

protected function getValidatedData(array $data) { // don't format this class since the rule:in should avoid space $validator = Validator::make($data, [ 'OnlyID' => 'required', 'Basisweight' => 'required', ]); if ($validator->fails()) { exit(Response::make(['message' => 'validation fails', 'errors' => $validator->errors()])); } return $data; }

I don't think exit() is a good way to return the errors message to clients. are there any other ways I can return the laravel Response to clients directly in an inner function. use throwing Exception?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire