public function create(Request $request) {
$this->validate($request, [ \'email\' => \'required|email\', ]);
$user
You can also try this.
public function create(Request $request) {
$this->validate($request, [ 'email' => 'required|email', ]);
$user = User::where('email', $request->email)->first();
if (!$user) {
return response()->json([
'message' => 'We can not find a user with that e-mail address.'
], 404);
}
}
Try with isEmpty()
, Like this :
public function create(Request $request) {
$validatedData = $request->validate([
'email' => 'required|email',
]);
$user = User::where('email', $request->email)->first();
if ($user->isEmpty()) {
return response()->json([
'message' => 'We can not find a user with that e-mail address.'
], 404);
} else {
return response()->json([
'message' => 'User is found'], 201 );
}
}
Try doing this.
This will return a success
with user found if user is actually in DB with email or return
404
with NO user found.
public function create(Request $request) {
$this->validate($request, [ 'email' => 'required|email', ]);
$user = User::where('email', $request->email)->first();
if ($user) {
return response()->json([
'message' => 'User Found'
], 200);
} else {
return response()->json([
'message' => 'We can not find a user with that e-mail address.'
], 404);
}
}
simply use the exists
method of validation
public function create(Request $request) {
$validatedData = $request->validate([
'email' => 'required|email|exists:user,email',
]);
}