Laravel Api Request validation

Last Updated: November 13, 2019

Laravel Api Request validation to get 402 status code.

You can use the following code when you validate your REST Api.

   public function update(Request $request)
    {
        $messages = [
            'firstname.required' => 'First Name is required',
            'lastname.required' => 'Last Name is required',
        ];
        $validator = Validator::make($request->all(), [
            'email' => 'required|unique:users|max:255',
            'firstname' => 'bail|required',
            'lastname' => 'bail|required',
        ],$messages);



        if ($validator->fails()) {

            return $this->respondWithError($validator->messages()->all(),Response::HTTP_UNPROCESSABLE_ENTITY);
           
        }else{
         // update code
        }
}