Today we will talk about how to validate email validation in laravel.When we create a register form and require validation on the email field.One way to register an email once you can not register the same email id.That is why we feel validation on the email field.The user validates the email field and informs that this email id is already exit.How is this validation implemented in laravel? You can read below.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use DB; use Validator; use Redirect; public function emaivalidation(Request $req) { $email = $req->email; $inputs = [ 'email' => $email, ]; $rules = [ 'email' => 'required|email|unique:users|max:50', ]; $messages = [ 'email.required' => 'Please enter email', 'email.unique' => 'This email already exits.', ]; $validation = Validator::make($inputs, $rules, $messages); if( $validation->fails() ){ return redirect()->back()->withInput()->with('errors', $validation->errors() ); } $user_id = DB::table('users')->insert([ 'email' =>$email ]); return redirect()->back()->with('message', 'Successfully.'); }