Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RootTech's avatar

Coupon code verification upon registration

I have a table on my database in coupons_code with randomly generated chars.

I want users to input a coupon code when registering, then the code should be verified that it matches a code on the coupons table before users registration is successful.

how do I go about it please.. I'm new to laravel and php,so please be as precise as possible.

0 likes
8 replies
Ashraam's avatar

Can you be more specific ?

What happens if the coupon is invalid or inexistant ?

1 like
RootTech's avatar

Invalid code error message should appear.

Cronix's avatar

Just write it out.

  1. Form has coupon field
  2. form submits
  3. controller does validation and uses exists rule to check db to see if coupon exists in the db https://laravel.com/docs/5.8/validation#rule-exists
  4. If not valid, redirect back to form and show validation error. This is pretty much automatic when using validation
  5. If valid, do whatever.
1 like
RootTech's avatar

Thanks Alot, it's the 3rd step I'm stuck at, the controller part... For the for, I have users_coupon and the coupons are stored in coupons _code....so how do I make the register controller check if the input from the form matches a value in coupons_code before making a successful registration.

Cronix's avatar

so how do I make the register controller check if the input from the form matches a value in coupons_code before making a successful registration.

$this->validate($request, [
    'coupon' => 'required|exists:coupons_code,fieldName'
]);

That assumes

  1. form field is named coupon
  2. db table name to check against coupons_code
  3. db field name in coupons_code table that has the value you're checking against is fieldName

Since you're new to all of this, I'd highly urge you to watch this series before starting to play with laravel, or you'll be asking a ton of questions. Also read the entire user guide from cover to cover. https://laracasts.com/series/laravel-from-scratch-2018

2 likes

Please or to participate in this conversation.