title: how to validate Unique database field on Controller in laravel
body: <p>Good Day, I am new in Laravel environment. I am developing a simple school enrollment registration website but having trouble in validation on my controller..</p>
  <p>My plan is, when the user will register in the website, username textbox should be validated if the username is already used by other student. i tried every possible tutorial i found in the net but i have no luck...</p>
  <p>Here is my page, red circle should be validated if the input username already exist in the database..
  <a href="https://i.stack.imgur.com/nxFSQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nxFSQ.png" alt="enter image description here" /></a></p>
  <p>Whole code in .blade</p>
  <pre><code>           &lt;!--Registration  start here --&gt;
             &lt;form method=&quot;Post&quot; action=&quot;{{url('store_account_Registration')}}&quot;&gt;
              @csrf 
             
              @if(Session::get('success'))
               &lt;div class=&quot;alert alert-danger&quot;&gt;
                   {{ Session::get('success')}}
               &lt;/div&gt;
              @endif
  
              @if(Session::get('fail'))
               &lt;div class=&quot;alert alert-danger&quot;&gt;
                   {{ Session::get('fail')}}
               &lt;/div&gt;
              @endif
             &lt;div class=&quot;col-sm-12 form-group&quot;&gt;
                &lt;input type=&quot;hidden&quot; class=&quot;form-control&quot; name=&quot;uniqID3&quot; id=&quot;uid&quot; placeholder=&quot;Enrollment Registration Number&quot;  value=&quot;{{ $regid3 }}&quot; &gt;
             &lt;/div&gt;
             &lt;div class=&quot;col-sm-12 form-group&quot;&gt;
               &lt;input type=&quot;hidden&quot; class=&quot;form-control&quot; name=&quot;pfulname3&quot; id=&quot;name-f&quot; placeholder=&quot;Enrollment Registration Number&quot; required value=&quot;{{ $fulname4 }}&quot; &gt;
             &lt;/div&gt;
             &lt;div class=&quot;container-fluid&quot; style=&quot;margin-bottom: 2em&quot;&gt;
              &lt;div class=&quot;row justify-content-center align-items-center&quot; style=&quot;padding: 10px&quot;&gt;
                 &lt;div class=&quot;card col-md-5&quot; style=&quot;transform: none;&gt;
                 
                       &lt;div class=&quot;card-body&quot;&gt;
                          &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot; style=&quot;margin-top: 1em&quot;&gt;
                            &lt;p&gt;Name: &lt;strong&gt;{{ $fulname4 }}&lt;/strong&gt;&lt;/p&gt;
                            &lt;p&gt;Enrollment No.: &lt;strong&gt;{{ $regid3 }}&lt;/strong&gt;&lt;/p&gt;
                          &lt;/div&gt;
                           
                              &lt;input type=&quot;hidden&quot; class=&quot;hide&quot; id=&quot;csrf_token&quot; name=&quot;csrf_token&quot; value=&quot;C8nPqbqTxzcML7Hw0jLRu41ry5b9a10a0e2bc2&quot;&gt;
                              &lt;div class=&quot;row&quot;&gt;
                                      &lt;div class=&quot;col-md-12&quot;&gt;
                                        &lt;div class=&quot;form-group&quot;&gt;
                                          &lt;label&gt;Username&lt;/label&gt;
                                          &lt;div class=&quot;input-group&quot;&gt;
                                            &lt;div class=&quot;input-group-prepend&quot;&gt;
                                              &lt;span class=&quot;input-group-text&quot;&gt;&lt;i class=&quot;fa fa-user&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;&lt;/span&gt;
                                            &lt;/div&gt;
                                            &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;username&quot; id=&quot;username&quot; placeholder=&quot;Username&quot; required value=&quot;{{ old('username')}}&quot;  &gt;
                                           
                                          &lt;/div&gt;
                                          &lt;div class=&quot;help-block with-errors text-danger&quot;&gt;
                                            &lt;span style=&quot;color:red&quot;&gt;@error('username'){{ $message}}@enderror&lt;/span&gt;
                                          &lt;/div&gt;
                                        &lt;/div&gt;
                                      &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class=&quot;row&quot;&gt;
                                    &lt;div class=&quot;col-md-12&quot;&gt;
                                      &lt;div class=&quot;form-group&quot;&gt;
                                        &lt;label&gt;Password&lt;/label&gt;
                                        &lt;div class=&quot;input-group&quot;&gt;
                                          &lt;div class=&quot;input-group-prepend&quot;&gt;
                                            &lt;span class=&quot;input-group-text&quot;&gt;&lt;i class=&quot;fa fa-lock&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;&lt;/span&gt;
                                          &lt;/div&gt;
                                          &lt;input type=&quot;password&quot; id=&quot;password&quot; name=&quot;password&quot; pattern=&quot;^\S{6,}$&quot; onchange=&quot;this.setCustomValidity(this.validity.patternMismatch ? 'Must have at least 6 characters' : ''); if(this.checkValidity()) form.password_two.pattern = this.value;&quot; class=&quot;form-control&quot;  title=&quot;Password is needed&quot; required placeholder=&quot;Password&quot; &gt;
                                          
                                        &lt;/div&gt;
                                        &lt;div class=&quot;help-block with-errors text-danger&quot;&gt;
                                          &lt;span style=&quot;color:red&quot;&gt;@error('password'){{ $message}}@enderror&lt;/span&gt;
                                        &lt;/div&gt;
                                      &lt;/div&gt;
                                    &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class=&quot;row&quot;&gt;
                                  &lt;div class=&quot;col-md-12&quot;&gt;
                                    &lt;div class=&quot;form-group&quot;&gt;
                                      &lt;label&gt;Confirm Password&lt;/label&gt;
                                      &lt;div class=&quot;input-group&quot;&gt;
                                        &lt;div class=&quot;input-group-prepend&quot;&gt;
                                          &lt;span class=&quot;input-group-text&quot;&gt;&lt;i class=&quot;fa fa-lock&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;&lt;/span&gt;
                                        &lt;/div&gt;
                                        &lt;input id=&quot;password_two&quot; type=&quot;password&quot; name=&quot;password_two&quot; pattern=&quot;^\S{6,}$&quot; onchange=&quot;this.setCustomValidity(this.validity.patternMismatch ? 'Please enter the same Password as above' : '');&quot; placeholder=&quot;Confirm Password&quot; class=&quot;form-control&quot;  title=&quot;Confirm Password is needed&quot; required placeholder=&quot;Confirm Password&quot; &gt;
                                        
                                      &lt;/div&gt;
                                      &lt;div class=&quot;help-block with-errors text-danger&quot;&gt;
                                        &lt;span style=&quot;color:red&quot;&gt;@error('password_two'){{ $message}}@enderror&lt;/span&gt;
                                      &lt;/div&gt;
                                    &lt;/div&gt;
                                  &lt;/div&gt;
                            &lt;/div&gt;
                              
                              &lt;div class=&quot;row&quot; style=&quot;margin-top: 1em&quot; &gt;
                                &lt;div class=&quot;col-md-12&quot; style=&quot;margin-bottom: 2em&quot;&gt;
                                  &lt;input type=&quot;hidden&quot; name=&quot;redirect&quot; value=&quot;&quot;&gt;
                                &lt;!--  &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary btn-lg btn-block&quot; name=&quot;submit&quot;&gt;Save OASIS Account&lt;/button&gt; --&gt;
                                 &lt;!--  &lt; &lt;a href=&quot;enrollment_success&quot;&gt;&lt;p class=&quot;btn btn-primary btn-lg btn-block&quot;&gt;Save OASIS Account&lt;/p&gt;&lt;/a&gt; --&gt;
                                  &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary btn-lg btn-block&quot;&gt;Save OASIS Account&lt;/button&gt; 
                                &lt;/div&gt;
                              &lt;/div&gt;
                           
                        &lt;/div&gt;
               &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
             &lt;!--Registration  start here --&gt;
          
      
          &lt;/div&gt;
        &lt;/form&gt; 
  enter code here
  </code></pre>
  <p>I always got this error..
  <a href="https://i.stack.imgur.com/IR8P6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IR8P6.png" alt="enter image description here" /></a></p>
  <p>Here is my code..</p>
  <ol>
  <li><p>my validation in my controller
  <a href="https://i.stack.imgur.com/EN1iv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EN1iv.png" alt="enter image description here" /></a>
  public function store_account_Registration(Request $request)
  {</p>
  <pre><code> request()-&gt;validate([
       'username' =&gt; 'required|min:6|unique:TempAccount,AccountName,'
   ]);
  </code></pre>
  <p>// $this-&gt;validator($request-&gt;all())-&gt;validate();</p>
  <pre><code> $current_date = date('Y-m-d H:i:s');
   $query = DB::table('TempAccount')-&gt;insert([
           'RegID'=&gt;$request-&gt;input('uniqID3'),
           'FullName'=&gt;$request-&gt;input('pfulname3'),
           'AccountName'=&gt;$request-&gt;input('username'),
           'Pass'=&gt;$request-&gt;input('password'),
           'created_at'=&gt; $current_date,
           'updated_at'=&gt; $current_date,
  
   ]);
  
  
  
   if($query){
       return back()-&gt;with('success', 'Data has been Successfyll inserted');
  
    //   return view('pages.enrollment_success');
  
  
      // return redirect()-&gt;route('pages.enrollment_GradeLevelSchoolInfo', ['uniqIDd' =&gt; 1]);
    //  return redirect('enrollment_GradeLevelSchoolInfo');
  
   }
   else
   {
       return back()-&gt;with('fail', 'something went wrong');
   }
  </code></pre>
  <p>}</p>
  </li>
  <li><p>my Route in web.php
  <a href="https://i.stack.imgur.com/kpQKI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kpQKI.png" alt="enter image description here" /></a></p>
  </li>
  </ol>
  <p>Route::get('pages',[accountregistration::class, 'index']);
  Route::Post('store_account_Registration', [accountregistration::class, 'store_account_Registration']);</p>
  <ol start="3">
  <li>my action in form..
  <a href="https://i.stack.imgur.com/VZ66P.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VZ66P.png" alt="enter image description here" /></a></li>
  </ol>