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

laravel-promocodes

> 编程语言
开源

Laravel 优惠券和促销码生成器。

568 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Laravel 优惠券和促销码生成器。

laravel-promocodes

Coupons and promotional codes generator for Laravel. Current release is only for Laravel 9.x and PHP 8.1. It's completely rewritten, and if you are using previous version, you should change your code accordingly. Code is simplified now and it should take you several minutes to completely rewrite usage.

Attention: Current version is completely rewritten. If you are missing some functionality, that was possible to achieve in previous versions, fill free to open issue. Hope this new version will be easier to use, and it will provide better functionality for your needs.

Installation

You can install the package via composer:

bash
composer require zgabievi/laravel-promocodes

Configuration

bash
php artisan vendor:publish --provider="Zorb\Promocodes\PromocodesServiceProvider"

Now you can change configurations as you need:

…

After you configure this file, run migrations:

bash
php artisan migrate

Now you will need to use AppliesPromocode on your user model.

php
namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Zorb\Promocodes\Traits\AppliesPromocode;

class User extends Authenticatable {
    use AppliesPromocode;

    //
}

Usage

It's very easy to use. Methods are combined, so that you can configure promocodes easily.

  • Reference
  • Creating Promocodes
  • Generating Promocodes
  • Applying Promocode
  • Expiring Promocode
  • Additional Methods

Reference

Name Explanation
Mask Astrisks will be replaced with random symbol
Characters Allowed symbols to use in mask replacement
Multi use Define if single code can be used multiple times, by the same user
Unlimited Generated code will have unlimited usages
Bound to user Define if promocode can be used only one user, if user is not assigned initially, first user will be bound to promocode
User Define user who will be initially bound to promocode
Count Amount of unique promocodes should be generated
Usages Define how many times can promocode be used
Expiration DateTime when promocode should be expired. Null means that promocode will never expire
Details Array of details which will be retrieved upon apply

Creating Promocodes

Using class

Combine methods as you need. You can skip any method that you don't need, most of them already have default values.

php
use Zorb\Promocodes\Facades\Promocodes;

Promocodes::mask('AA-***-BB') // default: config('promocodes.code_mask')
          ->characters('ABCDE12345') // default: config('promocodes.allowed_symbols')
          ->multiUse() // default: false
          ->unlimited() // default: false
          ->boundToUser() // default: false
          ->user(User::find(1)) // default: null
          ->count(5) // default: 1
          ->usages(5) // default: 1
          ->expiration(now()->addYear()) // default: null
          ->details([ 'discount' => 50 ]) // default: []
          ->create();

Using helper

There is a global helper function which will do the same as promocodes class. You can use named arguments magic from php 8.1.

php
createPromocodes(
    mask: 'AA-***-BB', // default: config('promocodes.code_mask')
    characters: 'ABCDE12345', // default: config('promocodes.allowed_symbols')
    multiUse: true, // default: false
    unlimited: true, // default: false
    boundToUser: true, // default: false
    user: User::find(1), // default: null
    count: 5, // default: 1
    usages: 5, // default: 1
    expiration: now()->addYear(), // default: null
    details: [ 'discount' => 50 ] // default: []
);

Using command

There is also the command for creating promocodes. Parameters are optional here too.

bash
php artisan promocodes:create\
  --mask="AA-***-BB"\
  --characters="ABCDE12345"\
  --multi-use\
  --unlimited\
  --bound-to-user\
  --user=1\
  --count=5\
  --usages=5\
  --expiration="2022-01-01 00:00:00"

Generating Promocodes

If you want to output promocodes and not save them to database, you can call generate method instead of create.

php
use Zorb\Promocodes\Facades\Promocodes;

Promocodes::mask('AA-***-BB') // default: config('promocodes.code_mask')
          ->characters('ABCDE12345') // default: config('promocodes.allowed_symbols')
          ->multiUse() // default: false
          ->unlimited() // default: false
          ->boundToUser() // default: false
          ->user(User::find(1)) // default: null
          ->count(5) // default: 1
          ->usages(5) // default: 1
          ->expiration(now()->addYear()) // default: null
          ->details([ 'discount' => 50 ]) // default: []
          ->generate();

Applying Promocode

Using class

Combine methods as you need. You can skip any method that you don't need.

php
use Zorb\Promocodes\Facades\Promocodes;

Promocodes::code('ABC-DEF')
          ->user(User::find(1)) // default: null
          ->apply();

Using helper

There is a global helper function which will do the same as promocodes class.

php
applyPomocode(
    'ABC-DEF',
    User::find(1) // default: null
);

Using command

There is also the command for applying promocode.

bash
php artisan promocodes:apply ABC-DEF --user=1

Exceptions

While trying to apply promocode, you should be aware of exceptions. Most part of the code throws exceptions, when there is a problem:

…

Events

There are two events which are fired upon applying.

php
// Zorb\Promocodes\Events\*

GuestAppliedPromocode // Fired when guest applies promocode
    // It has public variable: promocode

UserAppliedPromocode // Fired when user applies promocode
    // It has public variable: promocode
    // It has public variable: user

Expiring Promocode

Using helper

There is a global helper function which will expire promocode.

php
expirePromocode('ABC-DEF');

Using command

There is also the command for expiring promocode.

bash
php artisan promocodes:expire ABC-DEF

Trait Methods

If you added AppliesPromocode trait to your user model, you will have some additional methods on user.

php
$user = User::find(1);

$user->appliedPromocodes // Returns promocodes applied by user
$user->boundPromocodes // Returns promocodes bound to user
$user->applyPromocode('ABC-DEF') // Applies promocode to user

Additional Methods

php
Promocodes::all(); // To retrieve all (available/not available) promocodes
Promocodes::available(); // To retrieve valid (available) promocodes
Promocodes::notAvailable(); // To retrieve invalid (not available) promocodes

Testing

bash
composer test

Contributing

Please see CONTRIBUTING for details.

Credits

  • Zura Gabievi
  • All Contributors

License

The MIT License (MIT). Please see License File for more information.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

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

> 标签

PHPcouponspromo-codes

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

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言