PHP code example of particle-academy / fancy-mlm

1. Go to this page and download the library: Download particle-academy/fancy-mlm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

particle-academy / fancy-mlm example snippets


use FancyMlm\Plan\CompensationPlan;
use FancyMlm\Referral\ReferralEngine;

$plan = CompensationPlan::fromArray([
    'metric' => 'referral-bonus',
    'levelFactors' => [1.0, 0.5, 0.25],          // L1 100%, L2 50%, L3 25%
    'tiers' => ['default' => 1.0, 'silver' => 1.25, 'gold' => 1.5],
    'compression' => true,
]);

$engine = new ReferralEngine($plan, $yourMemberRepository, $yourRewardSink);

// A referred member just did something worth 100 — credit their upline:
$rewards = $engine->distribute(originMemberId: 'm-42', baseAmount: 100.0);
// gold sponsor at L1 earns 150; silver at L2 earns 62.5; default at L3 earns 25.

$unilevel = CompensationPlan::fromArray(['tree' => 'unilevel', 'levelFactors' => [1.0, 0.5, 0.25]]);
$binary   = CompensationPlan::fromArray(['tree' => 'binary',   'levelFactors' => [1.0, 0.5, 0.25]]);
$matrix   = CompensationPlan::fromArray(['tree' => 'matrix', 'width' => 3, 'levelFactors' => [1.0, 0.5, 0.25]]);

use FancyMlm\Laravel\Facades\Mlm;

// Distribute from a member, or from the member belonging to a user:
$rewards = Mlm::distribute($memberId, 100.0);
$rewards = Mlm::distributeForUser($userId, 100.0);

use FancyMlm\Laravel\Facades\Mlm;

// A member can hold many codes. The scope is yours: an opaque string, or a
// tuple that is encoded into one. The package gives it no meaning.
$code = Mlm::issueCode($memberId, 'spring-campaign');
$code = Mlm::issueCode($memberId, ['region-7', 'team-12'], expiresAt: now()->addMonths(3)->toDateTimeImmutable());

// Your route, your URL shape:
Route::get('/r/{code}', function (Request $request, string $code) {
    Mlm::recordScanFromRequest($request, $code);   // recorded | duplicate | code_inactive | unknown_code
    return redirect('/register');
})->middleware('throttle:referral-scans');

// After the new user and member exist:
$decision = Mlm::attributeSignupFromRequest($request, (string) $member->id);
// $decision->attributed, ->sponsorMemberId, ->code, ->reason

// Stats per code, scope or member over [from, to):
Mlm::stats('code', $from, $to, ['member' => (string) $member->id]);

RateLimiter::for('referral-scans', fn (Request $request) => [
    Limit::perMinute(60)->by('code:'.$request->route('code')),
    Limit::perMinute(20)->by('session:'.$request->session()->getId()),
]);

use FancyMlm\Attribution\SponsorCorrectionRequest;
use FancyMlm\Laravel\GateCorrectionAuthorizer;

// 1. You decide who may do it. Until you define this Gate, every correction is refused.
Gate::define(GateCorrectionAuthorizer::ABILITY, // 'fancy-mlm.correct-sponsor'
    fn (User $user, SponsorCorrectionRequest $request) => $user->can('manage-network'));

// 2. Correct it, with a reason and the acting user's id.
$correction = Mlm::correctSponsor($memberId, $newSponsorId, 'Ticket 4411: signed up through the wrong link', (string) auth()->id());

Mlm::sponsorCorrections($memberId); // the audit history, oldest first

Schedule::command('mlm:prune-scans')->daily();   // deletes scans older than retention_days (default 90)
bash
php artisan vendor:publish --tag=mlm-config      # config/mlm.php (the plan + tiers)
php artisan vendor:publish --tag=mlm-migrations  # mlm_members table  (or rely on auto-load)
php artisan migrate