百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

laravel-couponables

> 后端框架
开源

利用 Eloquent 多态关系的优惠码功能。

195 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

利用 Eloquent 多态关系的优惠码功能。

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 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPcouponcoupon-code-generatorcoupon-servicecoupons

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

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