Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

laravel-couponables

> 后端框架
Open source

Promocode functionality leveraging Eloquent polymorphic relationships.

195 stars0 likes1 views
WebsiteGitHub

About

Promocode functionality leveraging Eloquent polymorphic relationships.

Laravel Couponables

This package provides coupons/promocodes functionality for your Laravel application leveraging Eloquent's polymorphic relationships.

The package requires PHP 8.1 or higher and Laravel 10 or higher.


#StandWithUkraine


Installation

Install the package using composer:

bash
composer require michael-rubel/laravel-couponables

Publish the migrations:

bash
php artisan vendor:publish --tag="couponables-migrations"

Publish the config file:

bash
php artisan vendor:publish --tag="couponables-config"

Usage

After publishing migrations, apply a trait in the model you want to use as a $redeemer:

php
use HasCoupons;

Artisan command

You can add coupons to your database using Artisan command:

bash
php artisan make:coupon YourCouponCode

Optionally, you can pass the next arguments:

…

Adding coupons using model

You can as well add coupons simply using model:

php
Coupon::create([
    'code'  => '...',
    'type'  => '...'
    'value' => '...',
    ...
]);
  • Note: type and value columns are used for cost calculations (this is optional).
    If the type column is null, the subtraction strategy will be chosen.

Basic operations

Verify the coupon code:

php
$redeemer->verifyCoupon($code);

Redeem the coupon:

php
$redeemer->redeemCoupon($code);

Redeem the coupon in context of another model:

php
$redeemer
  ->redeemCoupon($code)
  ->for($course);

Combined redeemCoupon and for behavior (assuming the $course includes HasCoupons trait):

php
$course->redeemBy($redeemer, $code);

If something's going wrong, methods verifyCoupon and redeemCoupon will throw an exception:

…

If you want to bypass the exception and do something else:

php
$redeemer->verifyCouponOr($code, function ($code, $exception) {
    // Your action with $code or $exception!
});
php
$redeemer->redeemCouponOr($code, function ($code, $exception) {
    // Your action with $code or $exception!
});

Redeemer checks

Check if this coupon is already used by the model:

php
$redeemer->isCouponAlreadyUsed($code);

Check if the coupon is over the limit for the model:

php
$redeemer->isCouponOverLimit($code);

Coupon checks

php
public function isExpired(): bool;
public function isNotExpired(): bool;
public function isDisposable(): bool;
public function isOverQuantity(): bool;
public function isRedeemedBy(Model $redeemer): bool;
public function isOverLimitFor(Model $redeemer): bool;

This method references the model assigned to redeem the coupon:

php
public function redeemer(): ?Model;

Calculations

php
$coupon = Coupon::create([
    'code'  => 'my-generated-coupon-code-to-use',
    'type'  => CouponContract::TYPE_PERCENTAGE, // 'percentage'
    'value' => '10', // calc(using: 300); // 270.00

The package supports three types of item cost calculations:

  • subtraction - subtracts the given value from the value defined in the coupon model;
  • percentage - subtracts the given value by the percentage defined in the coupon model;
  • fixed - completely ignores the given value and takes the coupon model value instead.

Note: you can find constants for coupon types in the CouponContract

Listeners

If you go event-driven, you can handle package events:

  • CouponVerified
  • CouponRedeemed
  • CouponExpired
  • CouponIsOverLimit
  • CouponIsOverQuantity
  • NotAllowedToRedeem

Extending package functionality

Traits DefinesColumns and DefinesPivotColumns contain the methods that define column names to use by the package, so you can use inheritance to override them.

If you need to override the entire classes, use the config values or container bindings. All the classes in the package have their own contract (interface), so you're free to modify it as you wish.

CouponService has the Macroable trait, This way you can inject the methods to interact with the service without overriding anything.

Contributing

If you see any ways we can improve the package, PRs are welcome. But remember to write tests for your use cases.

Testing

bash
composer test

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPcouponcoupon-code-generatorcoupon-servicecoupons

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架