jijunair/laravel-referral

Laravel package for a referral system

v1.0.0 2023-07-13 06:42 UTC

This package is auto-updated.

Last update: 2024-04-26 02:56:54 UTC


README

Heading of Laravel Referral

Latest Version on Packagist Total Downloads License

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 jijunair/laravel-referral

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.

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!

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.