A simple system of referrals with the ability to assign different programs for different users.
This package was created based on the lesson author is Damir Miladinov, with some minor changes, for which I express my gratitude to him.
Via Composer
$ composer require pdazcom/laravel-referrals
Then in config/app.php add service-provider and facade alias:
'providers' => [
...
Pdazcom\Referrals\Providers\ReferralsServiceProvider::class,
...
];
First of all you need to run:
php artisan vendor:publish --tag=referrals-config
to make referrals.php
file in your config
folder.
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.
Add middleware to your web
group in Http/Kernel.php
:
'web' => [
...
\Pdazcom\Referrals\Http\Middleware\StoreReferralCode::class,
],
Add Pdazcom\Referrals\Traits\ReferralsMember
trait to your Users
model:
class User extends Authenticatable {
use ReferralsMember;
...
}
Then in Http/Controllers/Auth/RegisterController.php
add event dispatcher:
...
use Pdazcom\Referrals\Events\UserReferred;
...
// overwrite registered function
public function registered(Request $request)
{
// dispatch user referred event here
UserReferred::dispatch(request()->cookie('ref'), $user);
}
From this point all referral links would be attached new users as referrals to users owners of these links.
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
namespace App\ReferralPrograms;
use Pdazcom\Referrals\Programs\AbstractProgram;
class ExampleProgram extends AbstractProgram {
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();
}
}
create referral link:
php artisan tinker
Pdazcom\Referrals\Models\ReferralLink::create(['user_id' => 1, 'referral_program_id' => 1]);
and finally dispatch reward event in any place of your code:
use Pdazcom\Referrals\Events\ReferralCase;
...
ReferralCase::dispatch('example', $referralUser, $rewardObject);
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!
If you want to list all the users for a given Referral Link, simply use
$referralLink->referredUsers()
If you discover any security related issues, please email kostya.dn@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.