dimanche 7 juin 2020

How to change value if validation passed in laravel 5.8?

There is an invitation code in my app and I want it to be used only once after each use this code should change. so here is my code.

 protected function validator(array $data)
{
    $validator = Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'gender'=>['required','string'],
        'email' => ['string', 'email', 'max:255', 'nullable'],
        'mobile'=>['required','string','max:255'],
        'family_cast'=>['required','string','max:255'],
        'family_head'=>['required','string','max:255'],
        'gotra' => ['required','string','max:255'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
        'code'=>['required','string','exists:users,code']
    ]);

    if($validator->fails()){
        // do nothing
    }
    else{
               $varify = $validator;
        User::where('code',$data['code'])
                ->update(['code'=>rand(pow(10, 5-1), pow(10, 5)-1)]);
    }
    return $varify;
}

The value is getting changed but it does not validate the field. Because it compares it with older invitation value but invite code change after validation is passed. What should be done to do it in the correct way?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire