You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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();
});
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;
}
}
The text was updated successfully, but these errors were encountered:
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:
The text was updated successfully, but these errors were encountered: