title: laravel how to add validation rule based on custom database query
body: <p>I want the user to input postal code then in register controller I want to validate that postal code against postal code database I have so if the user provided postal code is in my database it will success despite of the formatting he/she used and what I mean by that is I want the validation to success whether he/she use spaces or no spaces at all.
  the query I would like to use:</p>
  
  <pre><code>$postalcode=$data['postal_code'];
      zipcodes::whereRaw('replace(POSTAL_CODE,\' \',\'\')=? ',[str_replace(' ','',$postal_code) 
  </code></pre>
  
  <p>I tried to use the following in my validation:</p>
  
  <pre><code>Rule::exists('zipcodes','POSTAL_CODE')-&gt;where(function ($query) use($postalcode) {
                //  die($postalcode);
                  $query-&gt;whereRaw('replace(POSTAL_CODE,\' \',\'\')=? ',[str_replace(' ','',$postalcode)]);
              })
  </code></pre>
  
  <p>but it does not do what I want and it seems to already try to find if postal code exist in zipcodes table before performing mysql replace or php str_replace.
  so please any suggestions?</p>