Simple Referrals system for Laravel
![Software License][ico-license]
![Total Downloads][ico-downloads], (*1)
A simple system of referrals with the ability to assign different programs for different users., (*2)
This package was created based on the lesson
author is Damir Miladinov, with some minor changes, for which I express my gratitude to him., (*3)
Installation
Add dependency
Via Composer, (*4)
``` bash
$ composer require pdazcom/laravel-referrals, (*5)
Then in config/app.php add service-provider and facade alias:
'providers' => [
...
Pdazcom\Referrals\Providers\ReferralsServiceProvider::class,
...
];, (*6)
### Configuration
First of all you need to run:
php artisan vendor:publish --tag=referrals-config, (*7)
to make `referrals.php` file in your `config` folder.
### Migrations
>**OPTIONAL:** If you want to make changes to the migration files, you also need to run:
>```
>php artisan vendor:publish --tag=referrals-migrations
>```
> Then change new migrations.
Run `php artisan migrate` to make tables in database.
### Middleware
Add middleware to your `web` group in `Http/Kernel.php`:
'web' => [
...
\Pdazcom\Referrals\Http\Middleware\StoreReferralCode::class,
],, (*8)
This intermediary stores referral links applied to the user in cookies.
>#### Note
>Starting from v2.0, several referral programs can be applied to same user.
>They will be stored in cookies as a JSON-object, and in the request instance,
>an array will be available in the `_referrals` property:
>```
>[
> 'ref_id_1' => 'expires_timestamp',
> 'ref_id_2' => 'expires_timestamp',
> ...
> 'ref_id_n' => 'expires_timestamp'
>]
>```
>where `ref_id_n` - ID of referral link, `expires_timestamp` - storage expire timestamp for links in cookies.
>
> Expired links are automatically deleted.
>
Add `Pdazcom\Referrals\Traits\ReferralsMember` trait to your `Users` model:
class User extends Authenticatable {
use ReferralsMember;
...
}
## Usage
### Add new referrer event
Then in `Http/Controllers/Auth/RegisterController.php` add event dispatcher:
...
use Pdazcom\Referrals\Events\UserReferred;
use Pdazcom\Referrals\Middlewares\StoreReferralCode;, (*9)
...
// overwrite registered function
public function registered(Request $request)
{
// dispatch user referred event here
UserReferred::dispatch(request()->input(StoreReferralCode::REFERRALS), $user);
}, (*10)
From this point all referral links would be attached new users as referrals to users owners of these links.
### Create referral program
And then you need to create a referral program in database and attach it to users by `referral_program_id` field:
php artisan tinker
Pdazcom\Referrals\Models\ReferralProgram::create(['name'=>'example', 'title' => 'Example Program', 'description' => 'Laravel Referrals made easy thanks to laravel-referrals package based on an article by Damir Miladinov,', 'uri' => 'register']);
add association to config `referrals.programs`:
...
'example' => App\ReferralPrograms\ExampleProgram.php
and create the reward class `App\ReferralPrograms\ExampleProgram.php` for referral program:
<?php, (*11)
namespace App\ReferralPrograms;, (*12)
use Pdazcom\Referrals\Programs\AbstractProgram;, (*13)
class ExampleProgram extends AbstractProgram {, (*14)
const ROYALTY_PERCENT = 30;
/**
* It can be anything that will allow you to calculate the reward.
*
* @param $rewardObject
*/
public function reward($rewardObject)
{
$this->recruitUser->balance = $this->recruitUser->balance + $rewardObject * (self::ROYALTY_PERCENT/100);
$this->recruitUser->save();
}
}, (*15)
create referral link:
php artisan tinker, (*16)
Pdazcom\Referrals\Models\ReferralLink::create(['user_id' => 1, 'referral_program_id' => 1]);, (*17)
and finally dispatch reward event in any place of your code:
use Pdazcom\Referrals\Events\ReferralCase;
..., (*18)
ReferralCase::dispatch('example', $referralUser, $rewardObject);, (*19)
From this point all referrals action you need would be reward recruit users by code logic in your reward classes.
Create many programs and their reward classes. Enjoy!
### Bonus Content
If you want to list all the users for a given Referral Link, simply use
```php
$referralLink->referredUsers()
Security
If you discover any security related issues, please email kostya.dn@gmail.com instead of using the issue tracker., (*20)
Credits
License
The MIT License (MIT). Please see License File for more information., (*21)