如果数据库中有重复的用户电子邮件,我想返回响应
我已经尝试使用子句“where”检查电子邮件,但返回的是 views/welcome.blade.php
我希望json响应为{成功:错误,错误:“电子邮件已注册”}
public function register(Request $request)
{
// VALIDATION FIELD
$this->validate($request, [
'name' => 'required',
'company_name' => 'required',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:8'
]);
try {
// DATA REQUEST
$data = [
'name' => $request->input('name'),
'company_name' => $request->input('company_name'),
'email' => $request->input('email'),
'password' => Hash::make($request->input('password'))
];
User::create($data);
// IF SUCCESS RETURN SUCCESS => TRUE
return response()->json([
'success' => true,
'data' => $data
]);
if(User::where('email', '=', $data->email )->exists()){
return response()->json([
'success' => false,
'error' => 'Email already registered, try another email'
]);
}else{
// CREATE DATA
User::create($data);
// IF SUCCESS RETURN SUCCESS => TRUE
return response()->json([
'success' => true,
'data' => $data
]);
}
}catch (\Throwable $e) {
// IF SUCCESS RETURN SUCCESS => False
return response()->json([
'success' => false,
'error' => "Error On Line {$e->getLine()}, <br>Message:<br>{$e->getMessage()}",
]);
}
}
宝慕林4294392