如果我忘记填写字段而不是显示 laravel 错误
If i forgot to fill field than not show laravel error
这是我注册控制器的代码...我通过 try catch 应用了代码,但在 postman 中仍然显示这样的错误
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格式显示。我怎样才能做到这一点?
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');
}
}
}
这是我注册控制器的代码...我通过 try catch 应用了代码,但在 postman 中仍然显示这样的错误
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格式显示。我怎样才能做到这一点?
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');
}
}
}