teikun-86/laravel-vouchers

允许用户兑换与模型绑定的券。

v1.0 2023-12-05 05:20 UTC

This package is auto-updated.

Last update: 2024-09-05 06:53:48 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

此包可以将券与您的Eloquent模型关联起来。如果您需要将券代码与存储在Eloquent模型中的内容关联起来,这可能很有用。

以下是如何创建和兑换券的示例

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

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

安装

您可以通过Composer安装此包

composer require beyondcode/laravel-vouchers

包将自动注册自己。

您可以使用以下命令发布迁移

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

发布迁移后,您可以通过运行迁移来创建券表

php artisan migrate

您可以使用以下命令发布配置文件

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

这是已发布配置文件的内容

<?php

return [

    /*
     * Database table name that will be used in migration
     */
    'table' => 'vouchers',

    /*
     * Database pivot table name for vouchers and users relation
     */
    'relation_table' => 'user_voucher',

    /*
     * List of characters that will be used for voucher code generation.
     */
    'characters' => '23456789ABCDEFGHJKLMNPQRSTUVWXYZ',

    /*
     * Voucher code prefix.
     *
     * Example: foo
     * Generated Code: foo-AGXF-1NH8
     */
    'prefix' => null,

    /*
     * Voucher code suffix.
     *
     * Example: foo
     * Generated Code: AGXF-1NH8-foo
     */
    'suffix' => null,

    /*
     * Code mask.
     * All asterisks will be removed by random characters.
     */
    'mask' => '****-****',

    /*
     * Separator to be used between prefix, code and suffix.
     */
    'separator' => '-',

    /*
     * The user model that belongs to vouchers.
     */
    'user_model' => \App\User::class,
];

用法

此包的基本概念是,您可以创建与特定模型关联的券。例如,您可能有一个在线视频课程销售应用程序,券可以为用户提供访问一个特定视频课程的权利。

BeyondCode\Vouchers\Traits\HasVouchers特性添加到所有您想与券关联的Eloquent模型中。

namespace App\Models;

use BeyondCode\Vouchers\Traits\CanRedeemVouchers;

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

此外,将BeyondCode\Vouchers\Traits\CanRedeemVouchers特性添加到您的用户模型中。这样,用户可以轻松兑换券代码,并且包会负责在数据库中存储券/用户关联。

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);

返回值是一个包含所有生成的Voucher模型的数组。

券模型有一个code属性,其中包含生成的券代码。

使用Eloquent模型

此外,您还可以通过在关联模型上使用createVouchers方法来创建券

$videoCourse = VideoCourse::find(1);

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

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

带有附加数据的券

将任意数据与您的券关联可能很有用——比如券创建者的个人消息等。在创建券时,您可以传递一个数组作为第二个参数,您可以在以后从券实例中检索它。

$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');

带有到期日期的券

您还可以创建只在特定日期之前可用的券。用户在此之后不能兑换此代码。《createVouchers》方法接受Carbon实例作为第三个参数。

$videoCourse = VideoCourse::find(1);

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

兑换券

让您的用户兑换券代码的最简单方法是使用用户模型上的redeemCode方法

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

如果券有效,该方法将返回与此代码关联的券模型。

如果您想兑换现有的券模型,您可以在用户模型上使用redeemVoucher方法

$user->redeemVoucher($voucher);

在用户成功兑换券后,此包将触发BeyondCode\Vouchers\Events\VoucherRedeemed事件。该事件包含用户实例和券实例。您应该监听此事件,以便在用户兑换券时执行应用程序的业务逻辑。

访问关联的券模型

券模型有一个指向关联的Eloquent模型的model关系。

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

$videoCourse = $voucher->model;

处理错误

redeemCoderedeemVoucher方法抛出一些异常,您将在应用程序中捕获并对其进行响应。

券无效

如果用户尝试兑换无效的代码,包将抛出以下异常:BeyondCode\Vouchers\Exceptions\VoucherIsInvalid

券已兑换

所有生成的代金券只能使用一次。如果用户尝试第二次使用代金券,或者另一个用户已经使用过此代金券,程序将抛出以下异常:BeyondCode\Vouchers\Exceptions\VoucherAlreadyRedeemed::class

代金券已过期

如果用户尝试使用过期的代金券代码,程序将抛出以下异常:BeyondCode\Vouchers\Exceptions\VoucherExpired

测试

composer test

更新日志

有关最近更改的更多信息,请参阅更新日志

贡献

有关详细信息,请参阅贡献指南

安全性

如果您发现任何安全问题,请通过marcel@beyondco.de发送电子邮件,而不是使用问题跟踪器。

鸣谢

此程序大量基于Zura Gabievi的Laravel Promocodes程序。代码可在GitHub上找到。

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件