i want people to leave there email for a newsletter and I thought a got it working, and it looks right, but must doing something wrong as it keeps going to the else statment even if there isnt a email in the database!
here is the code:
public function newsStore(Request $request)
{
$this->validate($request, [
'email' => 'required|max:155',
]);
$email = $request['email'];
$result = DB::table('newsletters')->where( 'email', '=', $email)->count();
if($result = 0){
$news = DB::table('newsletters')->insert(
[
'users_id' => 0,
'email' => request('email'),
]);
session()->flash('message', 'Thank you for your email!');
return back();
}else{
session()->flash('message', 'you have already given your email!');
return back();
}
}
and here is the code on the index page:
@guest
<form method="POST" action="/sub2">
{{ csrf_field() }}
<div class="input-group">
<input type="email" name="email" class="form-control" size="155" placeholder="For our News Letter Email Address" required>
<div class="input-group-btn">
<button type="submit" class="btn btn-danger">Subscribe</button>
</div>
</div>
</form>
@else
<form method="POST" action="/sub">
{{ csrf_field() }}
<div class="input-group">
<input type="email" name="email" class="form-control" size="155" placeholder="For our News Letter Email Address" required>
<div class="input-group-btn">
<button type="submit" class="btn btn-danger" >Subscribe</button>
</div>
</div>
</form>
@endguest