0

这是我的注册控制器代码...我通过 try catch 应用代码但在邮递员中仍然显示这样的错误

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');
        }

}
  

在 API 中,我忘记填写字段仍然记录提交并且不显示 laravel 错误,响应成功或失败以 json 格式显示。我怎样才能做到这一点?

4

1 回答 1

1
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');
  }
}
}
于 2019-11-25T09:29:02.387 回答