Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add the possibility to redeem vouchers multiple times with limit #57

Open
roesware opened this issue Jul 2, 2024 · 0 comments
Labels
good first issue Good for newcomers

Comments

@roesware
Copy link

roesware commented Jul 2, 2024

For example:

A school class (30 students) can redeem a code for their class.
But you want to count on how many times the voucher has been redeemd.

After 30 redeems, the voucher is no longer valid.

Code example:

  1. Migration
 Schema::create('vouchers', function (Blueprint $table) {
            $table->id();
            $table->string('code')->unique();
            $table->integer('max_redeems')->default(30); // new
            $table->integer('current_redeems')->default(0); // new
            $table->timestamps();
        });
  1. Model
class Voucher extends Model
{
    protected $fillable = ['code', 'max_redeems', 'current_redeems'];

    public function isValid()
    {
        return $this->current_redeems < $this->max_redeems;
    }

    public function redeem()
    {
        if ($this->isValid()) {
            $this->current_redeems++;
            $this->save();
            return true;
        }
        return false;
    }
}
@mechelon mechelon added the good first issue Good for newcomers label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants