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:
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:
For making multiple invite code which has one redemption and does not have any expiry date then use following method:
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 :
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);
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:
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:
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.
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.
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()
‘invite_code’ => ‘required|doorman:email’,
From any of the above packages you can easily implement the invite code functionality in your application using Laravel.