samedi 3 novembre 2018

laravel how to add validation rule based on custom database query

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:

$postalcode=$data['postal_code'];
    zipcodes::whereRaw('replace(POSTAL_CODE,\' \',\'\')=? ',[str_replace(' ','',$postal_code) 

I tried to use the following in my validation:

Rule::exists('zipcodes','POSTAL_CODE')->where(function ($query) use($postalcode) {
              //  die($postalcode);
                $query->whereRaw('replace(POSTAL_CODE,\' \',\'\')=? ',[str_replace(' ','',$postalcode)]);
            })

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?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire