DEV Community


How To Check Email Already Exist Or Not In Laravel

websolutionstuff profile image Websolutionstuff Originally published at ・2 min read

In this example i will show you how to check email already exist or not in laravel.

Many time user register with duplicate or exist email id and it is very difficult to maintain data or records in database so we will check email exist or not, 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.

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'));
Enter fullscreen mode Exit fullscreen mode

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">

            <span class="invalid-feedback" role="alert">
                <strong>{{ $message }}</strong>

<script src=""></script>  // if required
<script src="" type="text/javascript"></script>

    var email =  $("#email").val();
        rules: {            
            email: {
                required: true,
                email: true,
                remote: {
                    url: '{{url('user/checkemail')}}',
                    type: "post",
                    data: {
                        _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 E-Mail !..",
                remote: "Email address already in use!"
Enter fullscreen mode Exit fullscreen mode

And our code is completed here.

Read Also : Import Export CSV/EXCEL File In Laravel

Discussion (0)

Editor guide