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

laravel-vouchers

> 后端框架
Open source

Allow users to redeem vouchers that are bound to models.

705 stars0 likes2 views
WebsiteGitHub

About

Allow users to redeem vouchers that are bound to models.

Laravel Vouchers

This package can associate vouchers with your Eloquent models. This might come in handy, if you need to associate voucher codes with content that is stored in your Eloquent models.

Here is an example of how you can create vouchers and redeem them:

php
$videoCourse = VideoCourse::find(1);
$voucher = $videoCourse->createVoucher();

auth()->user()->redeemVoucher($voucher);

Installation

You can install the package via composer:

bash
composer require beyondcode/laravel-vouchers

The package will automatically register itself.

You can publish the migration with:

bash
php artisan vendor:publish --provider="BeyondCode\Vouchers\VouchersServiceProvider" --tag="migrations"

After the migration has been published you can create the vouchers table by running the migrations:

bash
php artisan migrate

You can publish the config-file with:

bash
php artisan vendor:publish --provider="BeyondCode\Vouchers\VouchersServiceProvider" --tag="config"

This is the content of the published config file:

…

If necessary, you can publish the translation files for further customization:

bash
php artisan vendor:publish --provider="BeyondCode\Vouchers\VouchersServiceProvider" --tag="translations"

You can access the translations of the package like so: __('vouchers::validation.code_invalid').

Usage

The basic concept of this package is that you can create vouchers, that are associated with a specific model. For example, you could have an application that sells online video courses and a voucher would give a user access to one specific video course.

Add the BeyondCode\Vouchers\Traits\HasVouchers trait to all your Eloquent models, that you want to be associated with vouchers.

php
namespace App\Models;

use BeyondCode\Vouchers\Traits\CanRedeemVouchers;

class User extends Authenticatable
{
    use CanRedeemVouchers;
    # ...
}

In addition, add the BeyondCode\Vouchers\Traits\CanRedeemVouchers trait to your user model. This way users can easily redeem voucher codes and the package takes care of storing the voucher/user association in the database.

php
namespace App\Models;

use BeyondCode\Vouchers\Traits\HasVouchers;

class VideoCourse extends Model
{
    use HasVouchers;
    # ...
}

Creating Vouchers

Using the facade

You can create one or multiple vouchers by using the Vouchers facade:

php
$videoCourse = VideoCourse::find(1);

// Create 5 vouchers associated to the videoCourse model.
$vouchers = Vouchers::create($videoCourse, 5);

The return value is an array containing all generated Voucher models.

The Voucher model has a property code which contains the generated voucher code.

Using the Eloquent model

In addition, you can also create vouchers by using the createVouchers method on the associated model:

php
$videoCourse = VideoCourse::find(1);

// Returns an array of Vouchers
$vouchers = $videoCourse->createVouchers(2);

// Returns a single Voucher model instance
$vouchers = $videoCourse->createVoucher();

Vouchers with additional data

It might be useful to associate arbitrary data to your vouchers - maybe a personal message from the person that created the voucher, etc. When creating the vouchers, you can pass an array as the second argument, which you can then retrieve later on the Voucher instance.

php
$videoCourse = VideoCourse::find(1);

$vouchers = $videoCourse->createVouchers(2, [
    'from' => 'Marcel',
    'message' => 'This one is for you. I hope you like it'
]);

$voucher = $user->redeem('ABC-DEF');
$from = $voucher->data->get('from');
$message = $voucher->data->get('message');

Vouchers with expiry dates

You can also create vouchers that will only be available until a certain date. A user can not redeem this code afterwards. The createVouchers method accept a Carbon instance as a third parameter.

php
$videoCourse = VideoCourse::find(1);

$videoCourse->createVouchers(2, [], today()->addDays(7));

Redeeming Vouchers

The easiest way to let your users redeem voucher codes is by using the redeemCode method on your User model:

php
$voucher = $user->redeemCode('ABCD-EFGH');

If the voucher is valid, the method will return the voucher model associated with this code.

In case you want to redeem an existing Voucher model, you can use the redeemVoucher method on your User model:

php
$user->redeemVoucher($voucher);

After a user successfully redeemed a voucher, this package will fire a BeyondCode\Vouchers\Events\VoucherRedeemed event. The event contains the user instance and the voucher instance. You should listen to this event in order to perform the business logic of your application, when a user redeems a voucher.

Accessing the vouchers associated model

The Voucher model has a model relation, that will point to the associated Eloquent model:

php
$voucher = $user->redeemCode('ABCD-EFGH');

$videoCourse = $voucher->model;

Validating Vouchers & Voucher Codes

The isValidCode and isValidVoucher methods on the Vouchers facade allow you to check if a voucher code is valid or if a voucher model is valid.

php
Vouchers::isValidCode('ABCD-EFGH'); // true or false
Vouchers::isValidVoucher($voucher); // true or false

Handling Errors

The redeemCode and redeemVoucher methods throw a couple of exceptions that you will want to catch and react to in your application:

Voucher invalid

If a user tries to redeem an invalid code, the package will throw the following exception: BeyondCode\Vouchers\Exceptions\VoucherIsInvalid.

Voucher already redeemed

All generated vouchers can only be redeemed once. If a user tries to redeem a voucher for a second time, or if another user already redeemed this voucher, the package will throw the following exception: BeyondCode\Vouchers\Exceptions\VoucherAlreadyRedeemed::class.

Voucher expired

If a user tries to redeem an expired voucher code, the package will throw the following exception: BeyondCode\Vouchers\Exceptions\VoucherExpired.

Testing

bash
composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

  • Marcel Pociot
  • All Contributors

This package is heavily based on the Laravel Promocodes package from Zura Gabievi. You can find the code on GitHub.

License

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

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPhacktoberfestlaravel

No comments yet. Be the first to share.

> Details

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

> Related tools

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