The "jijunair/laravel-referral" package is a custom Laravel package that provides referral code functionality for your Laravel applications. It allows you to generate referral codes, associate them with users, retrieve users based on their referral codes and all other related features.
You can install the package via Composer by running the following command:
composer require jijunair/laravel-referral
The package provides a configuration file that allows you to customize its behavior. You should publish the migration and the config/referral.php config file with:
php artisan vendor:publish --provider="Jijunair\LaravelReferral\Providers\ReferralServiceProvider"
After publishing, you can find the configuration file at config/referral.php.
Configuration Key | Description |
---|---|
cookie_name |
The name of the cookie that tracks referrals. |
cookie_expiry |
How long the referral cookie will be valid. (Default: 1 year) |
route_prefix |
The prefix used for referral links. |
ref_code_prefix |
The prefix added to the unique referral code for each user. |
redirect_route |
The page where users will go after clicking on a referral link. |
user_model |
The model class for the user. |
referral_length |
The length of the referral code for each user. (Default: 8 characters) |
These configuration options help customize the behavior of the referral system in your Laravel application. Feel free to adjust these values according to your preferences and requirements!
After the config and migration have been published and configured, you can create the tables for this package by running:
php artisan migrate
Add the necessary trait to your User model:
use Jijunair\LaravelReferral\Traits\Referrable;
class User extends Model
{
use Referrable;
}
To generate referral accounts for existing users, you can visit the following URL:
http://localhost:8000/generate-ref-accounts
This will generate referral codes for all existing users in your application.
To get the referrer of a user, you can use the following code:
use Illuminate\Support\Facades\Auth;
$user = Auth::user();
$referrer = $user->referralAccount->referrer;
This retrieves the referrer associated with the user.
To get the referrer by referral code, you can use the following code:
use Jijunair\LaravelReferral\Models\Referral;
use Illuminate\Support\Facades\Cookie;
$referralCode = Cookie::get(config('referral.cookie_name'));
$referrer = Referral::userByReferralCode($referralCode);
This retrieves the referrer based on the referral code stored in the cookie.
To check if a user has a referral account, you can use the following code:
$user->hasReferralAccount();
This returns true
if the user has a referral account, and false
otherwise.
To create a referral account for a user, you can use the following code:
$user->createReferralAccount($referrer->id);
This associates the user with the provided referrer by creating a referral account.
To get all referrals under a user, you can use the following code:
$referrals = $user->referrals;
This retrieves all the referrals associated with the user.
To get the referral link of a user, you can use the following code:
$referralLink = $user->getReferralLink();
This returns the referral link associated with the user.
Please see CHANGELOG for more information on what has changed recently.
Thank you for considering contributing to the Laravel Referral Package! If you have any suggestions, bug reports, or pull requests, please feel free to open an issue or submit a pull request on the GitHub repository.
The Laravel Referral Package is open-source software licensed under the MIT license.