How To Check Email Already Exist Or Not In Laravel

In this post we will see how to check email already exist or not in laravel.when user register with duplicate or exist email id at that time it is very difficult to maintain data or records in database so we will check email exist or not using email already exist validation in laravel, if email already register with same email id then we will display error message to user like email already exists.

Here i have created controller and added below code in my controller for how to check if email already exists in database in laravel.

 

Use Response;
public function userEmailCheck(Request $request)
{

	$data = $request->all(); // This will get all the request data.
	$userCount = User::where('email', $data['email']);
	if ($userCount->count()) {
		return Response::json(array('msg' => 'true'));
	} else {
		return Response::json(array('msg' => 'false'));
	}
}

Now create blade file for view and add javascript validation in this file.

<div class="form-group row">
	<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

	<div class="col-md-6">
		<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">

		@error('email')
			<span class="invalid-feedback" role="alert">
				<strong>{{ $message }}</strong>
			</span>
		@enderror
	</div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>  // if required
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/jquery.validate.min.js" type="text/javascript"></script>

<script>    
    var email =  $("#email").val();
    $('#registration').validate({
        rules: {            
            email: {
                required: true,
                email: true,
                remote: {
                    url: '{{url('user/checkemail')}}',
                    type: "post",
                    data: {
                        email:$(email).val(),
                        _token:"{{ csrf_token() }}"
                        },
                    dataFilter: function (data) {
                        var json = JSON.parse(data);
                        if (json.msg == "true") {
                            return "\"" + "Email address already in use" + "\"";
                        } else {
                            return 'true';
                        }
                    }
                }
            }
        },
        messages: {            
            email: {
                required: "Email is required!",
                email: "Enter A Valid EMail!",
                remote: "Email address already in use!"
            }
        }
    });
</script>

 


 

You might also like :

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS