laravelpostmanpostmortem-debuggingpostman-testcase

If i forgot to fill field than not show laravel error


This is my code of register controller...I applied code through try catch but in postman still show error like this

Symfony\Component\Debug\Exception\FatalThrowableError: Argument 2 passed to Symfony\Component\HttpFoundation\JsonResponse::__construct() must be of the type integer, string given, called in /home/ynvih0l26evc/public_html/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php on line 31 in file /home/ynvih0l26evc/public_html/vendor/symfony/http-foundation/JsonResponse.php on line 42

public function register(Request $request) {  
     try {
            $validator = Validator::make($request->all(), 
              [ 
              'user_type' => 'required',
              'fname' => 'required',
              'lname' => 'required',
              'dob' => 'required',
              'phone' => 'required',
              'gender' => 'required',
              'uname' => 'required',
              'email' => 'required|email',
              'password' => 'required',  
              'c_password' => 'required|same:password', 
             ]);   

$input = $request->all();  
 $input['password'] = bcrypt($input['password']);
 $user = User::create($input); 
 $success['token'] =  $user->createToken('AppName')->accessToken;
 $success['status'] = true;
 $success['data'] = [$user];
 $success['message'] ="User created successfully!";  
  return response()->json($success, $this->successStatus); 

 } catch(\Throwable $e) {
            // For php7
            \Log::error($e->getFile().' '.$e->getLine().': '.$e->getMessage());
            return response()->json(1000, 'Error');
        } catch(\Exception $e) {
            // For php5
            \Log::error($e->getFile().' '.$e->getLine().': '.$e->getMessage());
            return response()->json(1000, 'Error');
        }

}
  

In API i forgot to fill field still record submit and not show laravel error an response success or failure show in json format. How can i do that?


Solution

  • public function register(Request $request) {  
     $input = $request->all();
     $rules = array(
                'user_type' => 'required',
                  'fname' => 'required',
                  'lname' => 'required',
                  'dob' => 'required',
                  'phone' => 'required',
                  'gender' => 'required',
                  'uname' => 'required',
                  'email' => 'required|email',
                  'password' => 'required',  
                  'c_password' => 'required|same:password', 
            );
    $validator = Validator::make($input, $rules);
    if ($validator->fails()) {
             $arr = array("status" => 400, "message" => $validator->errors()->first(), "data" => array());
    } else {
    try {
    
     $input['password'] = bcrypt($input['password']);
     $user = User::create($input); 
     $success['token'] =  $user->createToken('AppName')->accessToken;
     $success['status'] = true;
     $success['data'] = [$user];
     $success['message'] ="User created successfully!";  
      return response()->json($success, $this->successStatus); 
    
     } catch(\Throwable $e) {
                // For php7
                \Log::error($e->getFile().' '.$e->getLine().': '.$e->getMessage());
                return response()->json(1000, 'Error');
            } catch(\Exception $e) {
                // For php5
                \Log::error($e->getFile().' '.$e->getLine().': '.$e->getMessage());
                return response()->json(1000, 'Error');
      }
    }
    }