PHPackages                             osoobe/laravel-promocodes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. osoobe/laravel-promocodes

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

osoobe/laravel-promocodes
=========================

Laravel 10+ compatible promotion codes generator and validator.

9.1.1(today)01↑2900%MITPHPPHP ^8.1|^8.2|^8.3

Since Oct 15Pushed todayCompare

[ Source](https://github.com/osoobe/laravel-promocodes)[ Packagist](https://packagist.org/packages/osoobe/laravel-promocodes)[ Docs](https://github.com/osoobe/laravel-promocodes)[ RSS](/packages/osoobe-laravel-promocodes/feed)WikiDiscussions release/9.0 Synced today

READMEChangelog (1)Dependencies (12)Versions (3)Used By (0)

[![#StandWithUkraine](https://camo.githubusercontent.com/2bedf63f24cda7efab02da955dc11fb7ef8a060e2f26b73c33a7aac84529b8a3/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f737570706f72742d756b7261696e652e7376673f743d31)](https://supportukrainenow.org)[![laravel-promocodes](https://camo.githubusercontent.com/d1e577a80600e8dc6ae77bb10ba12c63a4fc75f7e14d819723d4bc1d4c04f4d3/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f6c61726176656c2d70726f6d6f636f6465732e6a7065673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d7a676162696576692532466c61726176656c2d70726f6d6f636f646573267061747465726e3d746f706f677261706879267374796c653d7374796c655f32266465736372697074696f6e3d436f75706f6e732b616e642b70726f6d6f74696f6e616c2b636f6465732b67656e657261746f722e266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)](https://github.com/zgabievi/laravel-promocodes)

laravel-promocodes
==================

[](#laravel-promocodes)

[![Packagist](https://camo.githubusercontent.com/70558e6d07222ec037de90e060cee6fce4fe79bbdca788237eeb82eef2ffd224/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a676162696576692f70726f6d6f636f6465732e737667)](https://packagist.org/packages/zgabievi/promocodes)[![Packagist](https://camo.githubusercontent.com/02fae729287ed12f917969864b948db7a762aadfe43c681c73593c6290a6a809/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7a676162696576692f70726f6d6f636f6465732e737667)](https://packagist.org/packages/zgabievi/promocodes)[![license](https://camo.githubusercontent.com/b5ad8e0d8eafc3201514aeead7d6ac1b91dddf68d611c46b95021dc5e8ebd743/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7a676162696576692f70726f6d6f636f6465732e737667)](https://packagist.org/packages/zgabievi/promocodes)

Coupons and promotional codes generator for [Laravel](https://laravel.com). Current release is only for [Laravel 9.x](https://laravel.com/docs/9.x) and [PHP 8.1](https://www.php.net/releases/8.1/en.php). 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
------------

[](#installation)

You can install the package via composer:

```
composer require zgabievi/laravel-promocodes
```

Configuration
-------------

[](#configuration)

```
php artisan vendor:publish --provider="Osoobe\Promocodes\PromocodesServiceProvider"
```

Now you can change configurations as you need:

```
return [
    'models' => [
        'promocodes' => [
            'model' => \Osoobe\Promocodes\Models\Promocode::class,
            'table_name' => 'promocodes',
            'foreign_id' => 'promocode_id',
        ],

        'users' => [
            'model' => \App\Models\User::class,
            'table_name' => 'users',
            'foreign_id' => 'user_id',
        ],

        'pivot' => [
            'model' => \Osoobe\Promocodes\Models\PromocodeUser::class,
            'table_name' => 'promocode_user',
        ],
    ],
    'code_mask' => '****-****',
    'allowed_symbols' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789',
];
```

After you configure this file, run migrations:

```
php artisan migrate
```

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

```
namespace App\Models;

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

class User extends Authenticatable {
    use AppliesPromocode;

    //
}
```

Usage
-----

[](#usage)

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

- [Reference](#reference)
- [Creating Promocodes](#creating-promocodes)
- [Generating Promocodes](#creating-promocodes)
- [Applying Promocode](#applying-promocode)
- [Expiring Promocode](#expiring-promocode)
- [Additional Methods](#additional-methods)

Reference
---------

[](#reference)

NameExplanationMaskAstrisks will be replaced with random symbolCharactersAllowed symbols to use in mask replacementMulti useDefine if single code can be used multiple times, by the same userUnlimitedGenerated code will have unlimited usagesBound to userDefine if promocode can be used only one user, if user is not assigned initially, first user will be bound to promocodeUserDefine user who will be initially bound to promocodeCountAmount of unique promocodes should be generatedUsagesDefine how many times can promocode be usedExpirationDateTime when promocode should be expired. Null means that promocode will never expireDetailsArray of details which will be retrieved upon applyCreating Promocodes
-------------------

[](#creating-promocodes)

### Using class

[](#using-class)

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

```
use Osoobe\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

[](#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.

```
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

[](#using-command)

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

```
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

[](#generating-promocodes)

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

```
use Osoobe\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

[](#applying-promocode)

### Using class

[](#using-class-1)

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

```
use Osoobe\Promocodes\Facades\Promocodes;

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

### Using helper

[](#using-helper-1)

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

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

### Using command

[](#using-command-1)

There is also the command for applying promocode.

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

#### Exceptions

[](#exceptions)

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

```
// Osoobe\Promocodes\Exceptions\*

PromocodeAlreadyUsedByUserException - "The given code `ABC-DEF` is already used by user with id 1."
PromocodeBoundToOtherUserException - "The given code `ABC-DEF` is bound to other user, not user with id 1."
PromocodeDoesNotExistException - "The given code `ABC-DEF` doesn't exist." | "The code was not event provided."
PromocodeExpiredException - "The given code `ABC-DEF` already expired."
PromocodeNoUsagesLeftException - "The given code `ABC-DEF` has no usages left."
UserHasNoAppliesPromocodeTrait - "The given user model doesn't have AppliesPromocode trait."
UserRequiredToAcceptPromocode - "The given code `ABC-DEF` requires to be used by user, not by guest."
```

#### Events

[](#events)

There are two events which are fired upon applying.

```
// Osoobe\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

[](#expiring-promocode)

### Using helper

[](#using-helper-2)

There is a global helper function which will expire promocode.

```
expirePromocode('ABC-DEF');
```

### Using command

[](#using-command-2)

There is also the command for expiring promocode.

```
php artisan promocodes:expire ABC-DEF
```

Trait Methods
-------------

[](#trait-methods)

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

```
$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
------------------

[](#additional-methods)

```
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
-------

[](#testing)

```
composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/zgabievi/laravel-promocodes/blob/master/CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Zura Gabievi](https://github.com/zgabievi)
- [All Contributors](https://github.com/zgabievi/laravel-promocodes/graphs/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/zgabievi/laravel-promocodes/blob/master/LICENSE.md)for more information.

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance100

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 91.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

0d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1830494?v=4)[Oshane Bailey](/maintainers/b4oshany)[@b4oshany](https://github.com/b4oshany)

---

Top Contributors

[![zgabievi](https://avatars.githubusercontent.com/u/1515299?v=4)](https://github.com/zgabievi "zgabievi (11 commits)")[![b4oshany](https://avatars.githubusercontent.com/u/1830494?v=4)](https://github.com/b4oshany "b4oshany (1 commits)")

---

Tags

laraveldiscountscouponspromocodes

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/osoobe-laravel-promocodes/health.svg)

```
[![Health](https://phpackages.com/badges/osoobe-laravel-promocodes/health.svg)](https://phpackages.com/packages/osoobe-laravel-promocodes)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k120.0M598](/packages/barryvdh-laravel-ide-helper)[frittenkeez/laravel-vouchers

Voucher system for Laravel 9+

5818.8k1](/packages/frittenkeez-laravel-vouchers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
