asciisd/referrals-laravel
Composer 安装命令:
composer require asciisd/referrals-laravel
包简介
Adding referrals feature to your laravel project
README 文档
README
Referrals package for laravel
Compatible with laravel >= 8.0
Installation:
To install the package in your project you need to require it with composer.
$ composer require asciisd/referrals-laravel
Configs:
First, you need to set the package configurations by publishing the config file to your project config directory.
To publish the referrals.php config file use the publish command:
$ php artisan vendor:publish
and from the providers list choose the ReferralsLaravelServiceProvider.
Next, inside your .env file, add the route you want the invited user to be redirected to.
referral_route='route_name'
By default, the invited user will be redirected to the registration page with route register.
If you plan to use cookies to save the referral token, by default, the cookie life-time is set to 24 hours. You can customize
the cookie life-time by setting the referral_token_cookie_lifetime inside your .env file.
referral_token_cookie_lifetime=1440
Usage:
To use the package in your project, simply use the Referrable trait in your users model.
class User extends Authenticatable implements MustVerifyEmail{ /*...*/ use \Asciisd\ReferralsLaravel\app\Traits\Referrerable; /*...*/ }
Next, use php artisan migrate to migrate the database file. This migration wll create new table referrals. A one-to-many relationship
is created with the users table.
The referrer_id is the id of the user who sent the invitation link.
Saving the referral token to a cookie:
For further usage of the referral token, you can use ReferralsLaravel middleware inside your Kernel.php file.
You can set and alias to your middleware to use it with specific routes:
protected $middlewareAliases = [ /*...*/ 'referrals' => \Asciisd\ReferralsLaravel\app\Http\Middleware\ReferralsLaravel::class, ];
Or you can set it to any of your middleware groups:
protected $middlewareGroups = [ 'web' => [ /*...*/ \Asciisd\ReferralsLaravel\app\Http\Middleware\ReferralsLaravel::class, ], /*...*/ ]
Retrieving Referral Data:
To get all user referrals user referrer relationship property along with user object.
foreach ($user->referrals as $referral) { $referral->referral_token; }
To get user referral details from the referrals table use the property referrals.
// Get authenticated referral token auth()->user()->referral->referral_token;
To check if the user is invited by another user, use the isReferred() method on the user object.
// Return true if the user is invited // and false if the user was not invited by other user. $user->isReferred();
To check of the user has referral token or not use the hasReferralToken() method.
// Return true if the user has referral token, or false if not. $user->hasReferralToken();
To generate user referral token, use the generateReferalToken() method. The method
checks if the user has referral token or not, then it generated the referrals token.
$user->generateReferralToken();
To get the referral link you can either add referral_link attribute to the $append array inside
User model and use it as user attribute,
protected $append = ['referral_link'];
Or, you can use the getReferralLink() method with the user object.
// Get the referral link with the user redirect route // ex: https://mydomain.com/register?ref=34532234 $user->getReferralLink();
To get user referral token you can either add referral_token attribute to the $append array
inside the User model and use it as attribute,
protected $append = ['referral_token'];
Or, you can use the getReferralToken() method with the user object.
$user->getReferralToken();
Register with Nova
Add nova resource Referral will be automatically published during publishing the service provider, to use
the package with Nova just add the Referral resource to NovaServiceProvider file to register it inside your nova.
asciisd/referrals-laravel 适用场景与选型建议
asciisd/referrals-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 115 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 09 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 asciisd/referrals-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 asciisd/referrals-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 115
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-25