Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Gpanos's avatar

Multiple referral programs system

Hey all, I am working on integrating multiple referral programs into a website that will have different rewards and different triggers. Let's say for example a program that the referred user get's rewarded on signup with a voucher and the referrer get's rewarded after this user finishes his first booking much like Airbnb does. I made some progress already here are some snippets with a brief explanation: Referral programs migration&model

        Schema::create('referral_programs', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('slug');
            $table->integer('lifetime_minutes');
            $table->timestamps();
        })
<?php

namespace App;

use Illuminate\Support\Facades\Cookie;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
use Cviebrock\EloquentSluggable\SluggableScopeHelpers;

class ReferralProgram extends Model
{
    use Sluggable;
    use SluggableScopeHelpers;

    const DEFAULT_LIFETIME = 7 * 24 * 60;

    protected $fillable = [
        'name',
        'lifetime_minutes',
    ];

    protected $attributes = [
        'lifetime_minutes' => self::DEFAULT_LIFETIME,
    ];

    public function getRouteKeyName()
    {
        return 'slug';
    }

    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'name',
            ],
        ];
    }

    public function getUrlAttribute()
    {
        return action('ReferralController', $this->slug);
    }

    public function generateCookie(User $referrer)
    {
        return Cookie::make('referral', json_encode(['referrer' => $referrer->id, 'program' => $this->id]), $this->lifetime_minutes);
    }

    public function expires()
    {
        return now()->addMinutes($this->lifetime_minutes)->diffInDays();
    }
}

Each referral program has two landing pages one for the referrer to share the link and view the rewards for the program and one for the invited user where he can signup The link generation method in the User model:

    public function referralLink(ReferralProgram $program)
    {
        return $program->url.'?ref='.Hashids::encode($this->id);
    }

The routes:

    Route::get('referral/{program}', 'ReferralController')->middleware('guest');
    Route::get('invite/{program}', 'InviteController')->name('invite')->middleware('auth');

The controller where the guest lands to register:

class ReferralController extends Controller
{
    public function __invoke(Request $request, ReferralProgram $program)
    {
        $referrer = User::findOrFail(Hashids::decode($request->input('ref'))[0]);

        Cookie::queue($program->generateCookie($referrer));

        return view('pages.referrals.'.$program->slug, [
            'program' => $program,
            'referrer' => $referrer,
        ]);
    }
}

On user creation if the referral cookie is set

    public function created(User $user)
    {
        if ($referral = json_decode(Cookie::get('referral'), true)) {
            event(new UserReferred($user, $referral['referrer'], $referral['program']));
        }
    }

On UserReferred event create an entry in UserReferral which is model that simply holds the referred user , the referrer and the program

    public function handle(UserReferred $event)
    {
        $referral = UserReferral::create([
            'program_id' => $event->program->id,
            'referred_by' => $event->referrer->id,
            'user_id' => $event->user->id,
        ]);
    }

I think it's simple enough to do the job but I am currently stuck with the rewards. Since each program have a different reward strategy and every reward strategy had a different trigger for the referrer/user referred seems pretty hard to make it dynamic. I am thinking to create a Reward class for each program but this doesn't cover the case of the different reward per user... Am I going crazy and thinking it too much or my concept is wrong to start with? Do you guys have any feedback to put me back on track? Thanks a lot

0 likes
1 reply
Gpanos's avatar

Did some digging but really couldn't find any good resources around the subject of referrals a few old packages some old articles but not much which is kinda weird since referrals is such a common feature... A nice course in laracasts would be awesome!!

Please or to participate in this conversation.