Download the PHP package pdazcom/laravel-referrals without Composer

On this page you can find all versions of the php package pdazcom/laravel-referrals. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-referrals

Simple Referrals system for Laravel

Latest Version on Packagist Tests Total Downloads

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.

Installation

These steps are verified against a fresh Laravel 11, Laravel 12, and Laravel 13 application.

1. Install the package

Laravel registers the service provider automatically through package discovery.

2. Publish the config file

This creates config/referrals.php, where you register your referral program classes.

If you prefer the package command, you can run:

3. Run the migrations

The package loads its migrations automatically for the default setup, so you do not need to publish them unless you want to customize the migration files before running them.

If you need to customize the migrations, publish them first:

Or use the package command:

4. Register the middleware

In Laravel 11 and 12, append the middleware to the web stack in bootstrap/app.php:

This middleware stores referral links in cookies so they can be attached when the user signs up.

It accepts both legacy UUID links and human-friendly referral codes in the same ?ref= query parameter. For the recommended sharing patterns, see Sharing and Entry Flows.

5. Add the trait to your user model

Add Pdazcom\Referrals\Traits\ReferralsMember to app/Models/User.php:

Upgrade notes for older docs

Note

Starting from v2.0, several referral programs can be applied to the same user. They are stored in cookies as a JSON object, and the request instance exposes them in the _referrals property:

ref_id_n is the referral link ID, and expires_timestamp is the cookie expiration timestamp. Expired links are deleted automatically.

Next: continue with the quickstart to create your first referral program and verify the reward flow.

Documentation Map

Use the shortest guide that matches the task you are working on:

Document Use it when you need to
README.md Install the package, understand the core flow, or verify a first integration
docs/README.md Browse the docs by topic instead of searching the repo manually
docs/sharing-and-entry-flows.md Decide between share links, human-friendly codes, and manual code entry
docs/order-subscription-integration.md Reward referrers from order or subscription completion events
docs/fixed-reward-program.md Use or adapt the built-in fixed reward program
CONTRIBUTING.md Set up a local checkout, run tests, and open a pull request
docs/releases Review package changes by version
docs/research Read exploratory notes and design directions that are not source-of-truth setup docs

Configuration Reference

The package configuration file is config/referrals.php:

Keys

Key Default Required Behavior
programs ['example' => \Pdazcom\Referrals\Programs\ExampleProgram::class] Yes (for reward execution) Maps referral_programs.name to a reward handler class. RewardUser resolves handler classes via config('referrals.programs.<program_name>'). Missing mappings are skipped with a warning log.
fixed_reward_amount 10 No Default flat reward credited by Pdazcom\Referrals\Programs\FixedRewardProgram when you do not override its FIXED_AMOUNT constant in a subclass.
cookie_name 'ref' No Controls the query parameter read by StoreReferralCode and the cookie name used to persist active referral link IDs and expiry timestamps.
code_generator \Pdazcom\Referrals\Generators\RandomStringCodeGenerator::class No Container binding used to generate human-friendly referral_code values for new ReferralLink records.
code_length 8 No Length passed to the default random string code generator.
code_generation_max_attempts 10 No Maximum retries when generating a unique referral_code before throwing a ReferralCodeGenerationException.
prevent_duplicate_rewards false No When enabled, stamps referral_relationships.rewarded_at after the first payout and skips later payouts for the same relationship.
prevent_self_referral false No When enabled, ReferUser skips relationships where the referred user is also the owner of the referral link.
hooks.signup false No When true, automatically dispatches UserReferred on Illuminate\Auth\Events\Registered. Requires StoreReferralCode on the registration route.
hooks.first_purchase (see above) No When enabled is true and event is set, automatically dispatches ReferralCase when the configured event fires. See Reward Hooks for full options.

programs

Use programs to register each referral program name with the class that should run when ReferralCase is dispatched.

Example (single program):

Example (multiple programs):

cookie_name

cookie_name defines the referral parameter your links use and the cookie key the middleware writes to.

code_generator, code_length, and code_generation_max_attempts

Every new ReferralLink gets two codes:

The default generator is Pdazcom\Referrals\Generators\RandomStringCodeGenerator, which:

If you want custom code generation rules, bind a class that implements Pdazcom\Referrals\Contracts\ReferralCodeGeneratorInterface:

Use code_generation_max_attempts to cap the retry loop if your code space is small or highly constrained.

prevent_duplicate_rewards

Enable this guard when a referred user should only produce one payout for a given referral relationship:

Behavior:

Run the package migrations before enabling this guard so the rewarded_at column exists.

prevent_self_referral

Enable this guard if users must not attribute themselves with their own referral links:

Behavior:

Quickstart

This quickstart gives you a verified path from install to the first successful referral relationship and reward dispatch in a fresh Laravel 11 or 12 app.

1. Install the package

You do not need to publish the package migrations for the default setup. The package loads them automatically.

Checkpoint: php artisan about should show a Laravel Referrals section.

2. Register the middleware and trait

In Laravel 11 and 12, append the middleware in bootstrap/app.php:

Then add the trait to your app/Models/User.php model:

3. Add a reward handler

Create app/ReferralPrograms/QuickstartProgram.php:

Register it in config/referrals.php:

4. Create a referrer, program, and referral link

Checkpoint: the output includes a link_id, code, and url.

5. Dispatch the referral event for a new user

Checkpoint: relationship_exists is true.

6. Dispatch the reward event

Checkpoint: the log contains Quickstart reward triggered.

At this point the package is installed, the referral relationship is stored, and the reward handler is running. To wire this into your real registration flow, dispatch UserReferred::dispatch($request->input(StoreReferralCode::REFERRALS), $user) after signup as shown below.

If you want to support code sharing in chat, SMS, or native mobile flows, continue with Sharing and Entry Flows.

How It Works

The package is event-driven. This is the shortest path through the main objects:

Core models:

Integration choices:

Sharing and Entry Flows

Use this section to choose the referral flow that matches your product surface. The package now supports both shareable links and code-only attribution without breaking existing link-based integrations.

For a deeper guide with examples and verification steps, see docs/sharing-and-entry-flows.md.

Choose the right flow

Flow Best for What you share What the user does
referral_link Chat, email, SMS, landing pages Human-friendly link such as /register?ref=INVITE2024 Opens the link and signs up normally
referral_code Support flows, native mobile apps, offline campaigns Short code such as INVITE2024 Types or pastes the code into your app
link Backward-compatible integrations UUID link such as /register?ref=550e8400-e29b-41d4-a716-446655440000 Opens the legacy link

Share a human-friendly link

Use referral_link when you want a readable URL for public sharing:

This is the recommended default for web, email, SMS, and messaging apps because the same code can also be shown separately for manual entry.

Accept manual code entry

Use registerWithCode() when the referred user enters a code directly instead of visiting a link. The method accepts either the human-friendly referral_code or the legacy UUID code.

registerWithCode() returns true when the code resolves to a referral link and false when the code is unknown, so you can decide whether to show validation feedback or continue without attribution.

Keep legacy links if you already use them

The original link attribute still returns a URL with the UUID-based code:

This keeps older integrations working. New user-facing sharing surfaces should prefer referral_link and referral_code.

Verify attribution

  1. Create a referral link and note both $link->referral_link and $link->referral_code.
  2. Visit the share URL and confirm the middleware redirects to a clean URL and stores the referral cookie.
  3. Complete signup and confirm a referral_relationships row exists for the new user.
  4. Repeat the same attribution using $user->registerWithCode($link->referral_code) and confirm you get the same relationship result.

Reward Hooks

Reward hooks let you trigger referral events automatically in response to standard application events, without adding manual event dispatch to every controller or service. All hooks are opt-in and disabled by default.

Signup hook

The signup hook listens to Illuminate\Auth\Events\Registered and automatically dispatches UserReferred for any referral link stored in the current request by the StoreReferralCode middleware.

Requirements:

Enable in config/referrals.php:

When enabled, you no longer need to manually dispatch UserReferred in your registration controller. The hook handles it automatically as long as the referral cookie is present on the request.

First-purchase hook

The first-purchase hook listens to a configurable application event and dispatches ReferralCase for the configured programs. This is useful when you want to reward the referrer when a referred user makes their first purchase.

Enable and configure in config/referrals.php:

Options:

Key Default Description
enabled false Set to true to activate the hook.
event null Fully-qualified class name of the event to listen for. Must be set when enabled is true.
programs [] Array of referral program names to reward. Must match name values in referral_programs table.
user_accessor 'user' Property or zero-argument method name on the event that returns the referred Eloquent user model.
reward_accessor null Property or zero-argument method name on the event to use as the $rewardObject passed to ReferralCase. When null, the event object itself is passed.

Example event:

With the config above, ReferralCase::dispatch(['welcome-bonus', 'first-purchase'], $event->user, $event->order) is dispatched automatically whenever OrderCreated fires.

Note: The hook dispatches ReferralCase every time the configured event fires. If you want to reward only on the first purchase, add a guard inside your program's reward() method (for example, check whether a reward has already been recorded for this user).

Backward compatibility

Enabling hooks does not change any existing behavior. Existing manual dispatches of UserReferred and ReferralCase continue to work. You can keep manual dispatches alongside hooks without double-rewarding as long as you are not dispatching the same event twice for the same user action.

Manual Integration Flow

Use this section when you do not want to enable hooks and prefer to dispatch package events yourself.

1. Dispatch UserReferred after signup

This consumes the referral IDs that StoreReferralCode placed on the request and creates ReferralRelationship records through the ReferUser listener.

If you collect a typed referral code instead of relying on the middleware cookie, call:

2. Create the referral program record

Then map the program name to your reward handler in config/referrals.php:

3. Implement the reward class

4. Create a referral link for the recruiting user

The created model exposes:

5. Dispatch ReferralCase when the conversion happens

RewardUser will:

Bonus Content

If you want to list all the users for a given Referral Link, simply use

Contributing

See CONTRIBUTING.md for local setup, how to run tests, and the pull request workflow.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-referrals with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
laravel/framework Version ^9.52.18|^10|^11|^12|^13
ext-json Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package pdazcom/laravel-referrals contains the following files

Loading the files please wait ...