Custom request validator for API in Laravel

If you are working with APIs in Laravel then it’s obvious that you have to validate each request before executing your logic.

In the most cases, developer need to show their custom response code and message whenever a validation get fails.

For example:

You need to develop an API to add user then you must be use POST request and some information should be passed in form-data or as raw data.

Then you have to validate all data according to your rules and have to show respective errors like

In a separate file we have response codes and their messages like

Please check our post for creating API responder in Laravel in which I have shown to manage response codes and messages.

Now let’s create a custom validator library in app/Services directory.

Now here is how to use this validator in our controller

 

Please follow and like us:
20