Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bilal_mahoon's avatar

Custom request Error not sent in api response

i am using laravel 5.5 and have installed passport for Api's. when i hit a api with post man with wrong input then it will respond with index page expect of giving me request error response.

Any help will be much appreciated.

0 likes
6 replies
bilal_mahoon's avatar

route inside api.php : Route::post('sign-up', 'Api\UserController@signUp');

//controller function

public function signUp(SignUpRequest $request)

{

dd('inside controller);

}

//SignUpRequest rules

public function rules()

{

    return [
        'name' => 'required|min:1|max:190',
        'email' => 'required|email|max:190|unique:users',
        'password'  =>  'required|min:6|confirmed',
        'confirm_password'  =>  'required|min:6',

    ];

}

when i hit url from post man with wrong inputs that will redirect me to index page. i actually needed request error

bilal_mahoon's avatar

Actually the issue is, when anything goes for validation and respond in error then it will always goes for index page instead of giving me error. i think that i might needed some changes in request code to proceed with api??

RamjithAp's avatar

In your controller handle validation errors and then return as json for postman.

public function signUp(SignUpRequest $request)
{
 if ($validator->fails())
  {
     return response()->json(['status'=>'error','err_message'=>$validator->messages()->getMessages()]);
  } else {
   //do signup code here
   return response()->json(['status'=>'Signup successful.']);
 }
}
bilal_mahoon's avatar

This will only work if i do not use custom request and do validation inside controller because if we use custom request then it will only go inside controller after validation passes. But i don't want to do validation inside controller. i wanted to use Request

Please or to participate in this conversation.