code-8
code-8

Reputation: 58810

How can check for an existing email in Laravel?

I'm trying to check if the email is already exist my database on my subscribes table.


Form

{!! Form::open(array('url' => '/subscribe', 'class' => 'subscribe-form', 'role' =>'form')) !!}

<div class="form-group col-lg-7 col-md-8 col-sm-8 col-lg-offset-1">
  <label class="sr-only" for="mce-EMAIL">Email address</label>
  <input type="email" name="subscribe_email" class="form-control" id="mce-EMAIL" placeholder="Enter email" required>

  <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
  <div style="position: absolute; left: -5000px;"><input type="text" name="b_168a366a98d3248fbc35c0b67_73d49e0d23" value=""></div>
</div>
<div class="form-group col-lg-3 col-md-4 col-sm-4"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="btn btn-primary btn-block"></div>


{!! Form::close() !!}

Controller

public function postSubscribe() {

    // Validation
    $validator = Validator::make( Input::only('subscribe_email'),

        array(
            'email'  =>'unique:subscribes',
            )
        );

    if ($validator->fails()) {

        return Redirect::to('/#footer')
        ->with('subscribe_error','This email is already subscribed to us.')
        ->withErrors($validator)->withInput();

    }else{

        dd("HERE");

        $subscribe        = new Subscribe;
        $subscribe->email = Input::get('subscribe_email');
        $subscribe->save();

        return Redirect::to('/thank-you');

    }

}

Debuging Steps

I tried inputting email that I know already exist in my db. I want to my validation to fail, and redirect me back to my /#footer (homepage).

I try printing dd("HERE"); if my vaildation not fail.

BUT I keep getting HERE to print which mean my validation is not failing. Why is that happening ? I'm completely blank out now. Can someone please point out what I missed ? I know I am very close.

Thanks.

Upvotes: 0

Views: 8308

Answers (4)

Dachomo Lodam
Dachomo Lodam

Reputation: 11

//put this in your controller
    if (User::where('email', '=', Input::get('email'))->exists()) {


        return back()->withErrors([

        'message' => 'Email is already taken'

        ]);

      //then go up and include


  use Illuminate\Support\Facades\Input;

Upvotes: 0

Patrick Stephan
Patrick Stephan

Reputation: 1829

It looks like the reason this is not working is because you are trying to match the subscribe_email field against validation for a email field, no match is being made. To fix you need to do this:

$validator = Validator::make( Input::only('subscribe_email'),
    array(
        'subscribe_email'  =>'email|unique:subscribes,email',
    )
);

Upvotes: 0

Emeka Mbah
Emeka Mbah

Reputation: 17553

try specifying the column name of email in subscribes table

$rules = array(
'email' => 'email|unique:subscribes,email'
);

Upvotes: 1

Robert
Robert

Reputation: 5973

Your db field name is email not subscribe_email, your input param name however is. Laravel defaults to the fieldname given with the input, so in your case subscribe_email

Try this:

array(
    'subscribe_email'  => 'required|email|unique:subscribes,email',
)

This uses the db fieldname email, like you have, but the validation is on the field subscribe_email.

Upvotes: 4

Related Questions