- Notifications
You must be signed in to change notification settings - Fork32
MAINTANER REQUIRED: User (signup) invitation package for laravel
License
junaidnasir/larainvite
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Larainvite is a Laravel package that allows existing users to invite others via email.
It generates a one-time invitation code that can be sent as an invite link, and keeps track of the invitation status.
- v12 supports Laravel 12
- v7 supports Laravel 11
- v6 supports Laravel 10
- v5 supports Laravel 9
- v4 supports Laravel 8
- v3 supports Laravel 5.8+, Laravel 6.0 and Laravel 7.0
- v2 supports Laravel 5.0 to 5.8
- v1 supports Laravel 4.*
Begin by installing the package through Composer. Run the following command in your terminal:
composer require junaidnasir/larainvite
add the package service provider in the providers array inconfig/app.php
:
Junaidnasir\Larainvite\LaraInviteServiceProvider::class
you may add the facade access in the aliases array:
'Invite' =>Junaidnasir\Larainvite\Facades\Invite::class
publish the migration and config file:
php artisan vendor:publish --provider="Junaidnasir\Larainvite\LaraInviteServiceProvider"
migrate to createuser_invitation
table
php artisan migrate
edit yourUser
model to includelarainviteTrait
useJunaidnasir\Larainvite\InviteTrait;class user ... {use InviteTrait;}
You can usefacade accessor to retrieve the package controller. Examples:
$user = Auth::user();//Invite::invite(EMAIL, REFERRAL_ID);$refCode = Invite::invite('email@address.com',$user->id);//or//Invite::invite(EMAIL, REFERRAL_ID, EXPIRATION);$refCode = Invite::invite('email@address.com',$user->id,'2016-12-31 10:00:00');//or//Invite::invite(EMAIL, REFERRAL_ID, EXPIRATION, BEFORE_SAVE_CALLBACK);$refCode = Invite::invite($to, Auth::user()->id, Carbon::now()->addYear(1),function(/* InvitationModel, see Configurations */$invitation)use ($someValue) {$invitation->someParam =$someValue;});
now create routes withrefCode
, when user access that route you can use following methods
// Get route$code = Request::input('code');if( Invite::isValid($code)){$invitation = Invite::get($code);//retrieve invitation modal$invited_email =$invitation->email;$referral_user =$invitation->user;// show signup form}else {$status = Invite::status($code);// show error or show simple signup form}
// Post route$code = Request::input('code');$email = Request::input('signup_email');if( Invite::isAllowed($code,$email) ){// Register this user Invite::consume($code);}else {// either refCode is inavalid, or provided email was not invited against this refCode}
with help of new trait you have access to invitations sent by user
$user= User::find(1);$invitations =$user->invitations;$count =$user->invitations()->count();
larainvite fires severalevents
- Junaidnasir\Larainvite\Events\Invited
- Junaidnasir\Larainvite\Events\InvitationConsumed
- Junaidnasir\Larainvite\Events\InvitedCanceled
- Junaidnasir\Larainvite\Events\Expired
all of these events inclosesinvitation modal
so you can listen to these events.include listener inEventServiceProvider.php
useJunaidnasir\Larainvite\Events\Invited;useApp\Listeners\SendUserInvitationNotification;protected$listen = [ Invited::class => [ SendUserInvitationNotification::class, ],];
userInvite.php
publicfunctionhandle($invitation){ \Mail::queue('invitations.emailBody',$invitation,function ($m)use ($invitation) {$m->from('From Address','Your App Name');$m->to($invitation->email);$m->subject("You have been invited by".$invitation->user->name); });}
inconfig/larainvite.php
you can set default expiration time in hours from current time.
'expires' =>48
you can also change user model to be used, inlarainvite.php
'UserModel' =>'App\User'
you can also change invitation model to be used, inlarainvite.php
'InvitationModel' =>'App\Invitation'
About
MAINTANER REQUIRED: User (signup) invitation package for laravel