If you think to create a referral module. don't think, just clone and use. the Referral module is a pre-built and maintained module that provides all basic and necessary functionality for referral management in a Laravel project. The module includes features such as user retrieves the referral ID associated with the currently authenticated user, creates a new referral record, retrieves information about a user and the number of referrals they have made, updates an existing referral record in the database, delete a referral record ,update a referral status. By using this module, developers can save time and effort in implementing these common referral management features in their projects, while promoting consistency and standardization in module design and implementation.
composer require nwidart/laravel-modules
Step 1.1: Create Modules folder on root laravel project also register in composer.json
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/"
}
}
}
Tip: don't forget to run composer dump-autoload afterwards
Step 1.2: clone the code in modules folder
Tip: don't forget to run
php artisan module:enable ReferrerModule
Step 2:- install JWT Authentication official documentation
use Modules\ReferrerModule\Traits\ReferrerTraits;
class User extends Authenticatable implements JWTSubject
{
use HasApiTokens, HasFactory, Notifiable, ReferrerTraits;
}
protected function schedule(Schedule $schedule): void
{
$schedule->command('update:referrer-ids')->daily();
}
php artisan update:referrer-ids
- Retrieves the referral ID associated with the currently authenticated user
- creates a new referral record in the database
- updates an existing referral record in the database
- delete a referral record
- update a referral status
- retrieves information about a user
URL:- /api/get-referrer-id
Method:- GET
Request Body:-
Parameter | Type | Required | Description |
---|---|---|---|
token | string | Yes | JWT Token |
URL:- /api/referrers/store
Method:- POST
Request Body:- token (required),referrer_name (required), referrer_email (required , email)
referred_name (required, string), referred_email (required , email),
referred_code(required, valid)
Request Body:-
Parameter | Type | Required | Description |
---|---|---|---|
token | string | Yes | JWT Token |
referrer_name | string | Yes | Referrer Name |
referrer_email | Yes | Referrer Email | |
referred_name | string | Yes | Referred Name |
referred_email | Yes | Referred Email | |
referred_code | string | Yes | Referred Code |
URL:- /api/referrers/update/{id}
Method:- PUT
Request Body:-
Parameter | Type | Required | Description |
---|---|---|---|
token | string | Yes | JWT Token |
referrer_name | string | Yes | Referrer Name |
referrer_email | Yes | Referrer Email | |
referred_name | string | Yes | Referred Name |
referred_email | Yes | Referred Email | |
referred_code | string | Yes | Referred Code |
URL:- /api/referrers/{id}
Method:- DELETE
Request Body:-
Parameter | Type | Required | Description |
---|---|---|---|
token | string | Yes | JWT Token |
URL:- /api/update/referrer/status
Method:- POST
Parameter | Type | Required | Description |
---|---|---|---|
token | string | Yes | JWT Token |
id | integer | Yes | referral Id |
status | string | Yes | referral Status |
URL:- /api/user/referrers/{userId}
Method:- POST
Request Body:-
Parameter | Type | Required | Description |
---|---|---|---|
token | string | Yes | JWT Token |
php artisan test Modules/ReferrerModule/Tests/Unit/ReferrerModuleControllerTest.php