temian / referral-system
Laravel package for a referral system
Requires
- php: ^7.2|^7.3|^7.4|^8.0|^8.1|^8.2
- illuminate/auth: ^8.0|^9.0|^10.0|^11.0
- illuminate/database: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: ^9.4
This package is auto-updated.
Last update: 2024-12-05 15:57:01 UTC
README
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.
Installation
You can install the package via Composer by running the following command:
composer require temian/referral-system
Configuration
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.
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!
Migration
After the config and migration have been published and configured, you can create the tables for this package by running:
php artisan migrate
Add Trait
Add the necessary trait to your User model:
use Jijunair\LaravelReferral\Traits\Referrable; class User extends Model { use Referrable; }
Usage
Generate Referral Accounts for Existing Users
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.
Get the Referrer of a User
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.
Get Referrer by Referral Code
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.
Check if a User has a Referral Account
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.
Create a Referral Account for a User
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.
Get All Referrals of a User
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.
Get the Referral Link of a 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.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
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.
License
The Laravel Referral Package is open-source software licensed under the MIT license.