tasmidur / coupon
描述
v1.0.1
2022-07-27 05:04 UTC
Requires
- php: ^8.0
- illuminate/config: ^8.0|^9.0
- illuminate/database: ^8.0|^9.0
- illuminate/support: ^8.0|^9.0
This package is auto-updated.
Last update: 2024-09-10 21:22:18 UTC
README
此包可以将优惠券与您的Eloquent模型关联。如果您需要将优惠券代码与存储在Eloquent模型中的内容关联起来,这可能非常有用。
安装
您可以通过composer安装此包
composer require tasmidur/coupon
该包将自动注册自己。
您可以使用以下命令发布迁移:
php artisan vendor:publish --provider="Tasmidur\Coupon\LaravelCouponServiceProvider" --tag="coupon-migrations"
迁移发布后,您可以通过运行迁移来创建优惠券表:
php artisan migrate
您可以使用以下命令发布配置文件:
php artisan vendor:publish --provider=Tasmidur\Coupon\LaravelCouponServiceProvider --tag="config"
这是已发布的配置文件的内容
<?php return [ /* * Table that will be used for migration */ 'table' => 'coupons', /* * Model to use */ 'model' => \Tasmidur\Coupon\Models\Coupon::class, /* * Pivot table name for coupons and other table relation */ 'relation_table' => 'coupon_applied', /* * Pivot table model name for coupons and other table relation */ 'relation_model_class' => \App\Models\Course::class, /* * List of characters that will be used for Coupons code generation. */ 'coupon_mix_characters' => '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', /* * Coupons code prefix. * * Example: course2022 * Generated Code: course2022-37JH-1PUY */ 'prefix' => null, /* * Coupons code suffix. * * Example: course2022 * Generated Code: 37JH-1PUY-course2022 */ 'suffix' => null, /* * Separator to be used between prefix, code and suffix. */ 'separator' => '-', 'coupon_format'=>'*****-*****' ];
用法
此包的基本概念是您可以创建与特定模型关联的优惠券。例如,您可能有一个在线视频课程销售的应用程序,优惠券可以为用户提供访问特定视频课程的权限。
创建优惠券
使用外观
您可以使用 coupons
外观创建一个或多个优惠券,并通过它进行访问
- @method static array createCoupon(string $couponType, float $price, Carbon|null $expiredAt = null, int $totalAmount = 1)
- @method static mixed getCouponList(string $sortBy = "id", string $orderBy = "ASC")
- @method static mixed getCouponListWithPagination(int $length = 10, string $sortBy = "id", string $orderBy = "ASC")
- @method static bool deleteCoupon(int $id)
- @method static mixed getCoupon(int $id)
- @method static mixed updateCoupon(array $payload, int $id)
- @method static mixed check(string $code)
- @method static mixed whereApplyCoupon(string $code)
//Use for Create $coupon = Coupons::createCoupon(string $couponType, float $price, Carbon|null $expiredAt = null, int $totalAmount = 1); //Use for get Coupon List $coupon = Coupons::getCouponList(string $sortBy = "id", string $orderBy = "ASC"); $coupon = Coupons::getCouponListWithPagination(int $length = 10, string $sortBy = "id", string $orderBy = "ASC"); $coupon = Coupons::deleteCoupon(int $id); $coupon = Coupons::getCoupon(int $id); //Use for update Coupon List $coupon = Coupons::updateCoupon(array $payload, int $id); //Use for validity check of Coupon $coupon = Coupons::check(string $code); //return list of applied coupon where it applied $coupon = Coupons::whereApplyCoupon(string $code);
将 Tasmidur\Coupon\Traits\CouponTrait
特性添加到您的模型中。这样您可以轻松应用优惠券代码,并且包会负责在数据库中存储优惠券关联。
$course = Course::findOrFail($courseId); /** One Coupon Is for One Course */ $course->applyUniqueCoupon($couponCode); /** all applied coupons that is associated with course */ $coupons = Course::eloquentQuery($sortBy, $orderBy, $searchValue)->with(['category', 'coupons'])->get();
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。