Currently, invite code or sharing code has become very popular for marketing our application with large audience. Invite code is used to limit the start of our application, sharing code is also widely used for getting popularity and huge users on social media. And Laravel is also known for making flexible and scalable applications.
To implement invite code functionality in Laravel development, there is a very useful package called doorman.
It provides all the features of invite code like:
1. It can be used with only one email address.
2. It can be used for anyone. (Generally, used for social media sharing).
3. It contains limited as well as unlimited access for the usage.
4. It provides expiry date feature.
To install this package use following command.
composer require clarkeash/doorman
After it is installed, open config/app.php file, and put the provider in providers array as:
Clarkeash\Doorman\Providers\DoormanServiceProvider::class,
Also register alias in alias array as:
‘Doorman’ => Clarkeash\Doorman\Facades\Doorman::class,
Finally migrate the tables as:
php artisan migrate
Now we are ready to implement the invite codes. So let’s take a look on the basic functionality.
For making generic invites with one invite code and no expiry date and one redemption, then just write:
Doorman::generate()->make();
For making multiple invite code which has one redemption and does not have any expiry date then use following method:
Doorman::generate()->times(10)->make();
Here times() method defines the limit of invite code which we want.
Now if you want to make invite code which has limited number of redemption and there is no expiry date then use :
Doorman::generate()->uses(5)->make();
Here uses() method defines how many redemption can be used by the user.
Now if you want to make invite code with expiry date, then use the following method:
$date = Carbon::now()->addDays(30);
Doorman::generate()->expiresOn($date)->make();
Here expiresOn() method is used for giving the expiry date for invite code. And if you want to make a invite code that expires after given days then use:
Doorman::generate()->expiresIn(5)->make();
Here, expiresIn() method is used for number of days, after specified days, code will be expired.
If we want to make an invite code for a specific user, then use this method:
Doorman::generate()->for(‘[email protected]’)->make();
If we want to generate code for specific user, for() method can be used for it.Now we will have a look on how to redeem the code. You can redeem the code using redeem() method by specifying the invite code and also specially for specific user.
For example:
Doorman::redeem(’07DE8Y’);
// or
Doorman::redeem(’07DE8Y’, ‘[email protected]’’);
The Doorman has redeemed the code with increment in the redeem point by 1, and if it will fail to redeem, it throws the following exception:
InvalidInviteCode : When the code is invalid.
ExpiredInviteCode: When code is expired.
MaxUsesReached : When redemption limit is reached.
NotYourInviteCode: The code is not for specific user.
The Doorman also provides the checking functionality for invite code, you can check the code by providing a code and user. It will return true or false.
Doorman::check(‘8DFGT99’);
// or
Doorman::check(‘8DFGT99’, ‘[email protected]’);
So it is easy to check the code and then redeem it. If you are using form request and wants to validate the invite code before redeeming it, then you can also make validation rule in form request as follows:
public function rules()
{
return [
‘email’ =>’required|email|unique:users’,
‘invite_code’ => ‘required|doorman:email’,
];
}
From any of the above packages you can easily implement the invite code functionality in your application using Laravel.
About Author
Tarun Bansal - Technical Head
Tarun is a technology enthusiast with a flair for solving complex challenges. His technical expertise and deep knowledge of emerging trends have made him a go-to person for strategic tech initiatives. Passionate about innovation, Tarun continuously explores new ways to drive efficiency and performance in every project he undertakes.