Git Product home page Git Product logo

laravel-referral's Introduction

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.

laravel-referral's People

Contributors

jijunair avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

laravel-referral's Issues

does my referral code work with multiple users? I have shared the same link with five users does it work?

Before creating a new bug report
Please check if there isn't a similar issue on the issue tracker or in the discussions.

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior.

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.

Issue with user_model Configuration in Laravel Referral Package

Hi,

First, I want to thank you for your work on the Laravel Referral package. It’s been quite useful for my project.

However, I encountered an issue when trying to customize the user_model to use a different model than the default User model. I noticed that even after setting the user_model in the configuration file (referral.php), the package does not seem to reference this custom model. Instead, it always seems to use the model defined in config('auth.providers.users.model').

Here is the specific part of the code where I believe the issue arises:

$userModel = resolve(config('auth.providers.users.model'));

Because of this, the package does not correctly function with custom user models defined in the referral.php configuration file. To resolve this, it might be better to directly use the user_model setting from the package’s config file rather than the default authentication model.

Could you please look into this? I believe updating this would make the package more flexible and adaptable to various use cases.

Thanks again for your efforts and looking forward to any updates!

Best regards,
aliheidarian

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.